Scott Hanselman的问题-1

Scott Hanselman的问题

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

 

 

 

  过去几年都在忙着找项目,赶项目,没有时间好好整理深究自己在工作中学到的东西。现在好了,趁着找工作的这段空余时间,正好可以总结和再继续夯实自己的.Net, C#基本功。在05年的时候,Scott Hanselman(微软的一个Principal Program Manager)在他的博客上列出了一张清单, 清单上是关于"一个好的.Net程序员应该知道的东东 What Great .NET Developers Ought To Know (More .NET Interview Questions)". 昨天认真的把这张清单读过一遍之后, 发现自己还是有不少的问题根本不知道答案, 不少的问题只能给出个模糊的答案. 于是萌生一个想法, 不防花点时间把这些问题一个一个的回答一遍, 应该对自己对别人都会有帮助吧.

  说干就干. 他的清单里对所有问题分了六个大类. 接下来的几天里我就开始每天都回答一类的问题. 今天就先从C#语言篇开始.

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)?)
主互操作程序集(Primary Interop Assemblies)是一个由供应商提供的唯一的程序集。它包含用 COM 实现的类型的类型定义(作为元数据)。只能有一个主互操作程序集,而且该程序集必须由 COM 类型库的发行者用强名称签名。一个主互操作程序集可以包装同一类型库的多个版本。
如果导入为程序集的 COM 类型库不是由原类型库的发行者签名的,该类型库不能作为主互操作程序集。只有类型库的发行者才能产生真正的主互操作程序集。该程序集将成为用于与基础 COM 类型进行互操作的正式类型定义单元。
COM 组件的发行者生成主互操作程序集并将它们发布给开发人员以便在 .NET Framework 应用程序中使用。对于发行者,本节提供有关产生主互操作程序集的信息。对于开发人员,本节描述如何用主互操作程序集编程。
.
 
 
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 代指当前实例本身, 不可以用在静态函数中。因为静态函数不需要实例来调用的

 

原文地址:https://www.cnblogs.com/Leo_wl/p/8403497.html