私有成员变量的 C# 编码标准
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/743808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# coding standards for private member variables
提问by
I saw two common approaches for coding standards for private member variables:
我看到了私有成员变量编码标准的两种常见方法:
class Foo
{
private int _i;
private string _id;
}
and
和
class Foo
{
private int m_i;
private string m_id;
}
I believe the latter is coming from C++. Also, many people specify type before the member variable (e.g. double m_dVal
) to indicate that it is a non-constant member variable of the type double?
我相信后者来自 C++。另外,很多人在成员变量(例如double m_dVal
)之前指定 type来表示它是 double 类型的非常量成员变量?
What are the conventions in C#?
C#中的约定是什么?
回答by Daniel Schaffer
I prefer to use your first example, or auto-properties like this, which avoid having the private fields defined in your class at all:
我更喜欢使用你的第一个例子,或者像这样的自动属性,它完全避免在你的类中定义私有字段:
public String MyString { get; set; }
Use the prop
snippet to make these REALLY fast.
使用prop
代码片段使这些非常快。
回答by driis
Besides the two you mention, it is very common in C# to not have a prefix for private members.
除了你提到的两个,在 C# 中没有私有成员的前缀是很常见的。
class Foo
{
private int i;
private string id;
}
That is what I use, and also what is recommended in Microsoft's internal naming guidelines.
回答by AJ.
As I recall from reading Framework Design Guidelines, there really is no set convention for private member variables, except that one should not use Hungarian notation and one should not capitalize the first letter of the variable name (use Camel-casing). There are quotes in the book that support both of your examples, as well as not using any prefix at all.
我记得阅读框架设计指南时,对于私有成员变量确实没有固定的约定,除了不应该使用匈牙利表示法并且不应该大写变量名的第一个字母(使用驼峰式大小写)。书中有引用支持您的两个示例,并且根本不使用任何前缀。
Personally, I prefer the "m_" prefix.
就个人而言,我更喜欢“m_”前缀。
回答by Mehrdad Afshari
As noted by Brad Abrams: internal coding guidelines:
正如Brad Abrams所指出的:内部编码指南:
Do not use a prefix for member variables (
_
,m_
,s_
, etc.). If you want to distinguish between local and member variables you should use “this.
” in C# and “Me.
” in VB.NET.
不要使用成员变量的前缀(
_
,m_
,s_
等)。如果要区分局部变量和成员变量,this.
在 C# 中应使用“ ”,Me.
在 VB.NET 中应使用“ ” 。
回答by Jon Dewees
I tend to go with the first convention, a simple underscore. I also don't specify the type in the name, because I've got Intellisense telling me what it is (I know, it can be a crutch).
我倾向于采用第一个约定,一个简单的下划线。我也没有在名称中指定类型,因为我有智能感知告诉我它是什么(我知道,它可能是一个拐杖)。
Best to check with your coworkers or project teammates and just decide on a convention, some of them are kind of arbitrary.
最好与您的同事或项目团队成员核对,然后决定一个约定,其中一些是随意的。
回答by cyclo
I prefer not using any prefix at all for member variables. For those declared outside the method, I use "this.memberVariableName" to distinguish them from those declared inside the method.
我更喜欢对成员变量根本不使用任何前缀。对于在方法外声明的那些,我使用“this.memberVariableName”来区分它们与在方法内声明的那些。
回答by andleer
General guidance from Microsoft here:
微软的一般指导在这里:
http://msdn.microsoft.com/en-us/library/ms229002.aspx
http://msdn.microsoft.com/en-us/library/ms229002.aspx
Automatic properties in C# are great and I uses then when I can but there are cases where they don't work for me such as when doing type or value checking on a set method.
C# 中的自动属性很棒,我会尽可能使用它们,但在某些情况下它们对我不起作用,例如在对 set 方法进行类型或值检查时。
In general: use camel casing and don't prefix your name with anything such as underscore or a type prefix.
一般情况下:使用驼峰式大小写,不要在您的名字前加上下划线或类型前缀等任何前缀。
public int Age {get; set;}
or
或者
private int age;
public int Age
{
get { return age; }
set
{
if(value < 0)
throw new InvalidOperationException("Age > 0");
age = value;
}
}
回答by Brandon Montgomery
I think the important principle here is that you're consistent. Use a "_" or "m" prefix if that's what you like to do and it's consistent with the rest of the code you're working with. Whatever you choose, stick with it and be consistent.
我认为这里的重要原则是你要保持一致。如果这是您喜欢做的并且与您正在使用的其余代码一致,请使用“_”或“m”前缀。无论您选择什么,都要坚持并保持一致。
回答by ibz
Best is to use whatever is already used in the project. If you start a new project, use whatever is most often used in the company.
最好是使用项目中已经使用的任何东西。如果您开始一个新项目,请使用公司中最常用的任何内容。
回答by jheriko
In C++ identifiers beginning with _ are considered bad practice, they should be left for internal use. I think this is why prefixing a variable name with _ is sometimes considered bad practice in C# as well... although there is no reason why you can not do it since all of the .NET internals are encapsulated properly, unlike the C/C++ standard libraries.
在 C++ 中,以 _ 开头的标识符被认为是不好的做法,它们应该留给内部使用。我认为这就是为什么在 C# 中为变量名加上前缀 _ 有时也被认为是不好的做法......尽管没有理由不能这样做,因为所有 .NET 内部都被正确封装,与 C/C++ 不同标准库。