文章目录
大家好,这是自己自行整理的c#面试题,方便自己学习的同时分享出来。
using System;
public abstract class AbstProgram // 必须声明为抽象类
{
public abstract string abstMethod(); // 抽象方法
public virtual void virMethod() // 虚方法
{
Console.WriteLine("hello, virmethod");
}
}
public class Subclass: AbstProgram
{
// 抽象方法必须实现
public override string abstMethod()
{
return "hello,abstMethod";
}
// 虚方法可以实现,也可以选择不实现
//public override void virMethod()
//{
// Console.WriteLine("hello,virmethod");
//}
}
public interface IMyInterface // 接口
{
public void intMethod();
}
public abstract class AbstProgram // 抽象类
{
public abstract void abstMethod();
}
public class SubClass: AbstProgram, IMyInterface
{
public override void abstMethod()
{
Console.WriteLine("hello,abstMethod");
}
public void intMethod()
{
Console.WriteLine("hello,intMethod");
}
}
// 定义接口
interface IMyInterface
{
void MethodToImplement(); // 接口成员
}
class InterfaceImplementer: IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}
// 类实现接口
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}
public abstract class Program
{
public abstract string abstmethod(); // abstract
public virtual void virmethod() // virtual
{
Console.WriteLine("hello,virmethod");
}
}
public sealed class SubClass: Program // sealed
{
public override string abstmethod() // override
{
return "hello,abstmethod";
}
}
using System;
class Program
{
public static readonly int NumberA = NumberB * 10;
public static readonly int NumberB = 10;
public const int NumberC = NumberD * 10;
public const int NumberD = 10;
static void Main(string[] args)
{
Console.WriteLine("NumberA is {0}, NumberB is {1}.", NumberA, NumberB); //NumberA is 0, NumberB is 10.
Console.WriteLine("NumberC is {0}, NumberD is {1}.", NumberC, NumberD); //NumberC is 100, NumberD is 10.
Console.ReadLine();
}
}
public class Program
{
// 重载 (overload)
public void method(int num1)
{
}
public void method(string str1)
{
}
// 虚方法
public virtual void virtmethod()
{
Console.WriteLine("hello,virtmethod");
}
}
public class SubClass: Program
{
// 重写 (override)
public override void virtmethod()
{
Console.WriteLine("hello,overload");
}
}
// Books结构体实现
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
class Program
{
public void test1(ref int i)
{
i = 99;
}
public void test2(out int i)
{
i = 11;
}
static void Main(string[] args)
{
Program p = new Program();
int i = 1;
p.test1(ref i); // i = 99
int j;
p.test2(out j); // j = 10
}
}
//引用类型(由于使用了'Class')
class SomeRef { public Int32 x; }
//值类型(由于使用了'Struct')
struct SomeVal { public Int32 x; }
class Program
{
static void ValueTypeDemo()
{
SomeRef r1 = new SomeRef();
SomeVal v1 = new SomeVal();
r1.x = 5;
v1.x = 5;
Console.WriteLine(r1.x); //显示"5"
Console.WriteLine(v1.x); //同样显示"5"
SomeRef r2 = r1;
SomeVal v2 = v1;
r1.x = 8;
v1.x = 9;
Console.WriteLine(r1.x); //显示"8"
Console.WriteLine(r2.x); //显示"8"
Console.WriteLine(v1.x); //显示"9"
Console.WriteLine(v2.x); //显示"5"
}
}
public class Solution
{
static void Main(string[] args)
{
int i = 123; //声明一个int类型的变量i,并初始化为123
object obj = i; //执行装箱操作
Console.WriteLine("装箱操作:值为{0},装箱之后对象为{1}", i, obj);
int j = (int)obj; //执行拆箱操作
Console.WriteLine("拆箱操作:装箱对象为{0},值为{1}", obj, j);
}
}
delegate int NumberChanger(int n);
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int GetNum()
{
return num;
}
static void Main(string[] args)
{
// 创建委托实例
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
// 使用委托对象调用方法
nc1(25);
Console.WriteLine("Value of Num: {0}", GetNum()); // Value of Num: 35
nc2(5);
Console.WriteLine("Value of Num: {0}", GetNum()); // Value of Num: 175
Console.ReadKey();
}
}
public class Student
{
public int Age { get; }
public string Name { get; }
public Student()
{
Console.WriteLine("对象被创建。");
}
public Student(string name) : this()
{
this.Name = name;
}
public Student(string name, int age) : this(name)
{
this.Age = age;
}
}
public class Program
{
static void Main(string[] args)
{
Student student_1 = new Student();
Student student_2 = new Student("姓名", 14);
Console.WriteLine(student_2.Name + ' ' + student_2.Age);
}
}
class DemoProgram { };
sealed class SealedProgram: DemoProgram { };
public class Solution
{
static void Main()
{
var testTask = new Task(() =>
{
Console.WriteLine("hello, Task");
});
Console.WriteLine(testTask.Status); // Created
testTask.Start();
}
}
public class Solution
{
static void Main(string[] args)
{
ThreadTest test = new ThreadTest(); //创建ThreadTest类的一个实例
Thread thread = new Thread(new ThreadStart(test.MyThread)); //调用test实例的MyThread方法
thread.Start(); //启动线程
Console.ReadKey();
}
}
public class ThreadTest
{
public void MyThread()
{
Console.WriteLine("这是一个实例方法");
}
}
// where T:new()指明了创建T的实例时应该具有构造函数
class ItemFactory<T> where T : new()
{
public T GetNewItem()
{
return new T();
}
}
class Class1
{
};
class Prorgam
{
Class1 o = new Class1(); // 用于创建对象和调用构造函数
int myInt = new int(); // 也用于为值类型调用默认的构造函数
}
using System; // 引用命名空间;
using myClass1 = NameSpace1.MyClass; // 为命名空间或类型创建别名;
using myClass2 = NameSpace2.MyClass;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
myClass1 my1 = new myClass1();
Console.WriteLine(my1);
using (myClass2 my2 = new myClass2()) // 释放资源(关闭文件流);
{
Console.WriteLine(my2);
}
}
}
}
namespace NameSpace1
{
public class MyClass
{
public override string ToString()
{
return "hello, myClass1";
}
}
}
namespace NameSpace2
{
public class MyClass: IDisposable
{
public void Dispose()
{
throw new NotImplementedException();
}
public override string ToString()
{
return "hello, myClass2";
}
}
}
class Solution
{
static int Fib(int n)
{
if (n <= 0)
return 0;
if (n < 3)
return 1;
return Fib(n - 1) + Fib(n - 2);
}
static void Main(string[] args)
{
Console.WriteLine(Fib(30));
}
}
本章部分图文内容来源于各网站资料与网友提供, 笔者整理收集,方便学习参考,版权属于原作者。
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
前言作为一名程序员,自己的本质工作就是做程序开发,那么程序开发的时候最直接的体现就是代码,检验一个程序员技术水平的一个核心环节就是开发时候的代码能力。众所周知,程序开发的水平提升是一个循序渐进的过程,每一位程序员都是从“菜鸟”变成“大神”的,所以程序员在程序开发过程中的代码能力也是根据平时开发中的业务实践来积累和提升的。提高代码能力核心要素程序员要想提高自身代码能力,尤其是新晋程序员的代码能力有很大的提升空间的时候,需要针对性的去提高自己的代码能力。提高代码能力其实有几个比较关键的点,只要把握住这些方面,就能很好的、快速的提高自己的一部分代码能力。1、多去阅读开源项目,如有机会可以亲自参与开源
嗨~大家好,这里是可莉!今天给大家带来的是7个C语言的经典基础代码~那一起往下看下去把【程序一】打印100到200之间的素数#includeintmain(){ inti; for(i=100;i 【程序二】输出乘法口诀表#includeintmain(){inti;for(i=1;i 【程序三】判断1000年---2000年之间的闰年#includeintmain(){intyear;for(year=1000;year 【程序四】给定两个整形变量的值,将两个值的内容进行交换。这里提供两种方法来进行交换,第一种为创建临时变量来进行交换,第二种是不创建临时变量而直接进行交换。1.创建临时变量来