技术开发 频道

C# 语言经典面试题

    【IT168 评论】过去几年都在忙着找项目,赶项目,没有时间好好整理深究自己在工作中学到的东西。现在好了,趁着找工作的这段空余时间,正好可以总结和再继续夯实自己的.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修饰的同名方法所遮盖. 听起来有点抽象, 用下面代码演示一下, 就明了了.

public class Animal
     {
        
public virtual string DoSomething()
         {
            return
"I can breathe";          }
     }
  
    
public class Bird : Animal10     {
        
public override string DoSomething()
        {
             return
"I can fly";
         }
     }

    
public class Cat : Animal
     {
        
public new string DoSomething()
         {
             return
"I can run";
         }
     }


 

var animal = new Animal();
animal.DoSomething()    
// I can breathe
  var bird
= new Bird();
bird.DoSomething()      
//  I can fly
  var cat
= new Cat();
cat.DoSomething()   
//  I can run


 

var animal = new Animal();
animal.DoSomething()    
// I can breathe
  Animal bird
= new Bird();
bird.DoSomething()      
//  I can fly
  Animal cat
= new Cat();
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的例子

var bird = new Bird();
  
if (bird.GetType() == typeof(Animal))
{
    
// can not go in here
  }
  
  
if (bird is Animal)
{
    
// can go in here
      Console.WriteLine(
"bird is an Animal");
}

 

  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 代指当前实例本身, 不可以用在静态函数中。因为静态函数不需要实例来调用的

0
相关文章