1. 首页 > 电脑教程 > Scott清单中一个好的.net程序员应该知道的问题C#篇

Scott清单中一个好的.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 : Animal10 {11 public override string DoSomething()12 {13 return "I can fly";14 }15 }16 17 public class Cat : Animal18 {19 public new string DoSomething()20 {21 return "I can run";22 }23 }1 var animal = new Animal();2 animal.DoSomething() // I can breathe3 var bird = new Bird();4 bird.DoSomething() // I can fly5 var cat = new Cat();6 cat.DoSomething()   // I can run1 var animal = new Animal();2 animal.DoSomething() // I can breathe3 Animal bird = new Bird();4 bird.DoSomething() // I can fly5 Animal cat = new Cat();6 cat.DoSomething()   // I can breatheoverride 跟 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.0Culture: 这个程序集适用的文化环境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 here10 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 代指当前实例本身, 不可以用在静态函数中。因为静态函数不需要实例来调用的

声明:希维路由器教程网提供的内容,仅供网友学习交流,如有侵权请与我们联系删除,谢谢。ihuangque@qq.com
本文地址:https://www.ctrlcv.com.cn/diannao/169323125110632.html