zoukankan      html  css  js  c++  java
  • .Net 程序员面试 C# 语言篇 (转)

    过去几年都在忙着找项目,赶项目,没有时间好好整理深究自己在工作中学到的东西。现在好了,趁着找工作的这段空余时间,正好可以总结和再继续夯实自己的.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)?)
    这个问题我完全不懂, 上网找了一下解释, 也看不懂. 谁懂这个问题? 请赐教.
    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 代指当前实例本身, 不可以用在静态函数中。因为静态函数不需要实例来调用的

    转自:http://www.cnblogs.com/lipu/archive/2010/11/12/interview_questions_by_Scott_Hanselman.html?login=1#commentform

  • 相关阅读:
    Gitlab 自动化部署 + 局域网访问 gitlab pages
    Gitlab 跨版本升级
    Gitlab 私有化管理 npm 包
    Postman-请求加密和设置 Cookie
    menuStrip鼠标滑过自动弹出
    JAVA实用案例之文件导入导出(POI方式)
    springboot npoi 合并单元格 之后设置单元格居中
    postman测试导出Excel接口
    Application.DoEvents()的作用
    设置WINFORM窗体及程序图标
  • 原文地址:https://www.cnblogs.com/kevinlzf/p/1875441.html
Copyright © 2011-2022 走看看