您的位置:首页 > 职场人生

.Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题) 转

2010-11-15 10:05 316 查看
1.列出override跟new用法的不同. 什么是shadowing? (Juxtapose the use of override with new. What is shadowing?)

简单的讲, 子类的override, 将忽略父类用virtual修饰的同名方法. 但子类的new, 将被父类用virtual修饰的同名方法所遮盖. 听起来有点抽象, 用下面代码演示一下, 就明了了.

1     public class Animal
2     {
3         public virtual string DoSomething()
4         {
5             return "I can breathe";
6         }
7     }
8
9     public class Bird : Animal
10     {
11         public override string DoSomething()
12         {
13             return "I can fly";
14         }
15     }
16
17     public class Cat : Animal
18     {
19         public new string DoSomething()
20         {
21             return "I can run";
22         }
23     }


1 var animal = new Animal();
2 animal.DoSomething()    // I can breathe
3  var bird = new Bird();
4 bird.DoSomething()       //  I can fly
5  var cat = new Cat();
6 cat.DoSomething()   //  I can run


1 var animal = new Animal();
2 animal.DoSomething()    // I can breathe
3  Animal bird = new Bird();
4 bird.DoSomething()       //  I can fly
5  Animal cat = new Cat();
6 cat.DoSomething()   //  I can breathe


override 跟 new 的差别在上述运行结果中就现露无疑了. 当cat是Animal的时候, cat.DoSomething()只能用父类的函数. 但是bird.DoSomething()还是用自己override的函数.
2. 解释virtual, sealed, override 跟 abstract 的用法 (Explain the use of virtual, sealed, override, and abstract.)
virtual: 允许被子类重写.
sealed: 不允许被继承.
override: 在子类使用, 重写在父类中用virtual, abstract 或 override修饰的函数.
abstract: 只能被继承, 不能被实例化. 函数本身不能有实现代码, 但是可以有属性
3. 解释Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d 这行里每个部分的重要性跟用法 (Explain the importance and use of each component of this string: Foo.Bar, Version=2.0.205.0, Culture=neutral, PublicKeyToken=593777ae2d274679d)
Foo.Bar: Assembly(程序集)的名字,
Version: 版本号, 就像 ASP.MCV 1.0, 2.0, 3.0
Culture: 这个程序集适用的文化环境
PublicKeyToken: 原作者在发布此程序集的时候生成, 用来鉴别这个程序集是否被别人修改过
4. 解释public, protected, private, internal 的不同 (Explain the differences between public, protected, private and internal)
public: 所有的地方都能调用
protected: 自己跟子类可以用
private: 只能自己的类里面用
internal: 只能当前程序集里用
protected internal: 是指protected or internal的用法
5. 使用Primary Interop Assembly (PIA)的好处是什么? (What benefit do you get from using a Primary Interop Assembly (PIA)?)
这个问题我完全不懂, 上网找了一下解释, 也看不懂. 谁懂这个问题? 请赐教.
6. UNite 是通过什么机制知道要测试哪一个方法的? (By what mechanism does NUnit know what methods to test?)
从来没有想过这个问题, 参考了一下网上的答案, 是通过attributes. 其他人有什么更加详细的解释吗?
7. catch(Exception e){throw e;} 和 catch(Exception e){throw;}的区别 (What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;})
前者不保留原先的stacktrace, 后者保留原先的stacktrace.
8. What is the difference between typeof(foo) and myFoo.GetType()?
typeof(foo), foo 是类, 在编译的时候执行,myFoo.GetType(), myFoo 是类的一个实列,在运行时执行。沿用上面Bird跟Animal的例子

1 var bird = new Bird();
2  if (bird.GetType() == typeof(Animal))
3 {
4     // can not go in here
5  }
6
7  if (bird is Animal)
8 {
9     // can go in here
10      Console.WriteLine("bird is an Animal");
11 }


9. Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?
public a(string a) : this() {} 调用base constructor public c(){}. 好处是当base constructor c()有逻辑时(e.g. 初始化域)可以避免重复代码。
10. "This" 是什么?可以用在static函数中吗? (What is this? Can this be used within a static method?)
this 代指当前实例本身, 不可以用在静态函数中。因为静态函数不需要实例来调用的
文章来源:
/article/5485035.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: