总结一下,有两个基本的思路:
以下面的例子为例
public class Class1{
public string Prop1{
get {return m_Prop1;}
set {m_Prop1 = value; }
}
private string m_Prop1; // This is standard private property variable name.
// How do we cap this variable name? While the compiler can figure out same casing
// it makes it hard to read.
private Class2 Class2;
// We camel case the parameter.
public Class1(Class2 class2){
this.Class2 = class2;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的股票规则
m_
必须表明这一点.我的同事更喜欢_
.如果使用m_
或_
应该使用它会有一些争论,因为它就像匈牙利的符号.我试图弄清楚的部分是当私有字段的类名与私有字段名称匹配时我该怎么办.例如,私有Class2 Class2; .这令人困惑.
如果私有字段名称不是同一个类,例如私有字符串Name; ,没有太多问题.
或者我是以错误的方式思考问题?我的课程和私人领域是否应该以不会发生碰撞的方式命名?
===
下面的共识是使用小写的私有属性名称,但后来我们遇到了这个问题.
class1{
private string name; // This is scoped to the class.
public void Set(){
string firstName; // This is scoped to the method.
.... // Lot of code.
// Should use this.name but nothing prevents.
// Now there is confusion if name is a class property or scoped to the method.
name = firstName;
}
Run Code Online (Sandbox Code Playgroud)
或者ReSharper的指导方针.
私人财产与任何其他财产一样.私有字段是小写字母,以下划线开头.
private string _foo = string.Empty;
private string Bar { get; set; }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2876 次 |
最近记录: |