西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)C#.NET → Scott 清單中一個好的.net程序員應(yīng)該知道的問題C#篇

Scott 清單中一個好的.net程序員應(yīng)該知道的問題C#篇

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2010/11/19 9:01:25字體大小:A-A+

作者:佚名點擊:25次評論:1次標(biāo)簽: Scott .net

  • 類型:遠(yuǎn)程監(jiān)控大。4.6M語言:中文 評分:5.7
  • 標(biāo)簽:
立即下載

過去幾年都在忙著找項目,趕項目,沒有時間好好整理深究自己在工作中學(xué)到的東西。現(xiàn)在好了,趁著找工作的這段空余時間,正好可以總結(jié)和再繼續(xù)夯實自己的.Net, C#基本功。在05年的時候,Scott Hanselman(微軟的一個Principal Program Manager)在他的博客上列出了一張清單, 清單上是關(guān)于"一個好的.Net程序員應(yīng)該知道的東東 What Great .NET Developers Ought To Know (More .NET Interview Questions)". 昨天認(rèn)真的把這張清單讀過一遍之后, 發(fā)現(xiàn)自己還是有不少的問題根本不知道答案, 不少的問題只能給出個模糊的答案. 于是萌生一個想法, 不防花點時間把這些問題一個一個的回答一遍, 應(yīng)該對自己對別人都會有幫助吧.

  說干就干. 他的清單里對所有問題分了六個大類. 接下來的幾天里我就開始每天都回答一類的問題. 今天就先從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 的差別在上述運行結(jié)果中就現(xiàn)露無疑了. 當(dāng)cat是Animal的時候, cat.DoSomething()只能用父類的函數(shù). 但是bird.DoSomething()還是用自己override的函數(shù).

2. 解釋virtual, sealed, override 跟 abstract 的用法 (Explain the use of virtual, sealed, override, and abstract.)
virtual: 允許被子類重寫.
sealed: 不允許被繼承.
override: 在子類使用, 重寫在父類中用virtual, abstract 或 override修飾的函數(shù).
abstract: 只能被繼承, 不能被實例化. 函數(shù)本身不能有實現(xiàn)代碼, 但是可以有屬性

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: 這個程序集適用的文化環(huán)境
PublicKeyToken: 原作者在發(fā)布此程序集的時候生成, 用來鑒別這個程序集是否被別人修改過

4. 解釋public, protected, private, internal 的不同 (Explain the differences between public, protected, private and internal)
public: 所有的地方都能調(diào)用
protected: 自己跟子類可以用
private: 只能自己的類里面用
internal: 只能當(dāng)前程序集里用
protected internal: 是指protected or internal的用法

5. 使用Primary Interop Assembly (PIA)的好處是什么? (What benefit do you get from using a Primary Interop Assembly (PIA)?)
這個問題我完全不懂, 上網(wǎng)找了一下解釋, 也看不懂. 誰懂這個問題? 請賜教.

6. UNite 是通過什么機制知道要測試哪一個方法的? (By what mechanism does NUnit know what methods to test?)
從來沒有想過這個問題, 參考了一下網(wǎng)上的答案, 是通過attributes. 其他人有什么更加詳細(xì)的解釋嗎?

7. catch(Exception e){throw e;} 和 catch(Exception e){throw;}的區(qū)別 (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 是類, 在編譯的時候執(zhí)行,myFoo.GetType(), myFoo 是類的一個實列,在運行時執(zhí)行。沿用上面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() {} 調(diào)用base constructor public c(){}. 好處是當(dāng)base constructor c()有邏輯時(e.g. 初始化域)可以避免重復(fù)代碼。

10. "This" 是什么?可以用在static函數(shù)中嗎? (What is this? Can this be used within a static method?)
this 代指當(dāng)前實例本身, 不可以用在靜態(tài)函數(shù)中。因為靜態(tài)函數(shù)不需要實例來調(diào)用的

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(1)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)