.Net 程式員面試 C# 語言篇 (回答Scott Hanselman的問題)

來源:互聯網
上載者:User

  過去幾年都在忙著找項目,趕項目,沒有時間好好整理深究自己在工作中學到的東西。現在好了,趁著找工作的這段空餘時間,正好可以總結和再繼續夯實自己的.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.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 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 代指當前執行個體本身, 不可以用在靜態函數中。因為靜態函數不需要執行個體來調用的
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.