2010-08-19 19 views
1

試圖通過這本書做的一切,這是正確的?:C#風格的問題,構造函數的變量名和類屬性名

public class JollyHockey 
{ 
    public string AnotherCoolProperty { get { return anotherCoolProperty; } } 

    string anotherCoolProperty; 

    public JollyHockey(string anotherCoolProperty) 
    { 
     this.anotherCoolProperty = anotherCoolProperty; 
    } 
} 

或者做一些人使用強調了私有類變量?

+1

下載ReSharper試用版並使用它幾個星期。這推動了各種行業標準的命名規則和次要的重構,包括你在這裏談論的內容。 – fletcher 2010-08-19 20:08:39

回答

4

有些人(包括我自己)用下劃線(簡稱爲中正在使用的其中一個視覺指示)前綴私有類變量。

這主要是個人(或團隊)級別的風格考慮,你可以使用你想要的(或你的團隊已經標準化)。

只要確定你是一致的!

對於它的價值,你也可以使用自動屬性的例子:

public class JollyHockey 
{ 
    public string AnotherCoolProperty { get; private set; } 
    public JollyHockey(string anotherCoolProperty) 
    { 
     AnotherCoolProperty = anotherCoolProperty; 
    } 
} 
+0

從未聽說過汽車性能。太棒了! – 2010-08-19 21:37:50

4

或者,你可以這樣做:

public class JollyHockey 
{ 
    public string AnotherCoolProperty { get; private set; } 

    public JollyHockey(string anotherCoolProperty) 
    { 
     this.AnotherCoolProperty = anotherCoolProperty; 
    } 
} 
+0

+1用於暗示私人二傳手。我比Justin Niessner更喜歡你的回答,因爲用下劃線給私有變量加前綴看起來真的很醜。 :) – jloubert 2010-08-19 21:09:14

0

我相信,你的榜樣與MS編碼指引一​​致。但是,我不喜歡它,這是你的團隊可以同意的。

我不喜歡它的原因是因爲基礎字段名稱經常與方法參數衝突。我使用下劃線清楚地表明它們是私有變量。

0

當一個函數的參數具有相同的名稱作爲一個私人領域,

我通常用下劃線前綴參數

我覺得很有道理

ReSharper的事情弗萊徹是一個很好的想法