c#问答
—-VS自动生成的代码为
namespace ConsoleApplication1
{
class Program
{
….
}
}
class Program的访问权限为? 能否定义为private 或 protected?
—-构造函数是否可以为virtual? why?
—-c#中的overloading函数是否可以仅仅只有返回值不同,why?
(在调用函数,并忽略其返回值时, 你能说出是在调用哪个函数吗?)
能不能参数列表完全相同,仅仅用ref或out来区别?
—-有没有特例?
public static explicit operator Int32(Rational r){}
public static explicit operator Single(Rational r){}
实际生成的代码在metadata中的记录为:
public static Int32 op_Explicit(Rational r)
public static Single op_Explicit(Rational r)
CLR可以仅通过返回值识别函数,c++,c#,vb,java都不支持.
—-为什么要定义缺省构造器?
//v1
public class Foo
{
}
//v2
pubolc class Foo
{
public Foo(int value)
}
此时已有的代码会出现什么问题?
—-C++的构造函数中能否抛出异常,c#呢?why?此时GC的行为如何?
—-How about this code snippet?
class a
{
public a(int c)
{
}
}
class b : a
{
public b(int c) //: base(c)
{ }
}
—-How about this one?
class a
{
public a()
{ }
}
class b : a
{
public b()
{ }
}
A:
class a
{
public a()
{ }
public a(int c)
{
}
}
class b : a
{
public b(int c)
{ }
}
Will creat code in b.b(int c) to call a()
—-能否在finally block中return一个值? why?
—-下面的代码返回值为?
private static int GetInt()
{
string a = null;
try
{
Console.WriteLine(a.Length);
return 10;
}
finally
{
Console.WriteLine(”finally block”);
}
return 100;
}
(因为没有catch,所以不会有返回值.)
如果去掉Console.WriteLine(a.Length); return 10 和 finally谁先执行?
(return 10 先执行)
—-What is the difference:
try
{}
catch()
{
throw;
}
try
{}
catch()
{
throw new Exception();
}
—-Do you like using:
try
{}
catch(Excption e)
{}
why?
