C# 以_开头的变量名是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/549606/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:47:10  来源:igfitidea点击:

What does variable names beginning with _ mean?

c#asp.netsyntaxvariablesnaming-conventions

提问by Niyaz

When writing my first asp.net MVC application using C#, I see that there are some variables whose name start with an underscore character(_).

在使用 C# 编写我的第一个 asp.net MVC 应用程序时,我看到有些变量的名称以下划线字符 (_) 开头。

What does this mean? Is there any specific meaning for this?

这是什么意思?这有什么特别的意义吗?

采纳答案by Jon Skeet

There's no language-defined meaning - it's just a convention some people use to distinguish instance variables from local variables. Other variations include m_foo (and s_foo or g_foo or static variables) or mFoo; alternatively some people like to prefix the local variables (and parameters) instead of the instance variables.

没有语言定义的含义——它只是一些人用来区分实例变量和局部变量的约定。其他变体包括 m_foo(和 s_foo 或 g_foo 或静态变量)或 mFoo;或者,有些人喜欢为局部变量(和参数)而不是实例变量添加前缀。

Personally I don't use prefixes like this, but it's a style choice. So long as everyone working on the same project is consistent, it's usually not much of an issue. I've seen some horriblyinconsistent code though...

我个人不使用这样的前缀,但这是一种风格选择。只要在同一个项目上工作的每个人都是一致的,通常就不是什么大问题。不过,我已经看到了一些非常不一致的代码......

回答by Ben Robbins

In general, this means private member fields.

通常,这意味着私有成员字段。

回答by Steven Robbins

A lot of people use them for property private variables (the variables that actually store the values for public properties).

很多人将它们用于属性私有变量(实际存储公共属性值的变量)。

回答by ShuggyCoUk

I have seen it used as a variable name for parameters which are not being used as a way to 'hide' them away from the rest of the function.

我已经看到它用作参数的变量名,这些参数没有被用作将它们从函数的其余部分“隐藏”的方法。

I can't say this is a good or bad thing but it was effective at indicating that the parameter was not to be used in the function.

我不能说这是好事还是坏事,但它有效地表明该参数不会在函数中使用。

回答by Raktim Biswas

The underscore before a variable name _valis nothing more than a convention. In C#, it is used when defining the private member variable for a public property.

变量名前的下划线_val只不过是一种约定。在 C# 中,它在为公共属性定义私有成员变量时使用。

I'll add to what @Steven Robbinssaid:

我会补充@Steven Robbins所说的:

private string _val;
public string Values
{
    get { return _val;}
    set {_val = value;}
}

回答by Carlos A Merighe

There is another advantage with starting an instance property with '', it shows up first on Intellisense.
When creating a value/model/POCO class, I don't using '
'.

使用“
启动实例属性还有另一个优势,它首先出现在 Intellisense 上。创建值/模型/POCO 类时,我不使用“”。

回答by Lalo Téllez

I know this is a little old, but in case anyone ends up here I'll add to the answers here that if you are talking about MVC on .NET, also there is a naming convention on partial views also with their names starting with underscore.

我知道这有点旧,但如果有人最终到这里,我会在这里添加答案,如果您在谈论 .NET 上的 MVC,那么部分视图也有一个命名约定,它们的名称也以下划线开头.

This are naming conventions that you and your team can decide to follow or not, as long as all of you are aware of the decision. I use them also with non public fields like private or protected just to recognize them.

这是您和您的团队可以决定是否遵循的命名约定,只要你们所有人都知道该决定。我也将它们用于非公共字段,例如 private 或 protected 只是为了识别它们。

Here's a little brief about itif you want to read further.

如果您想进一步阅读,这里有一些简短的介绍

回答by Hamish Anderson

The use of an underscore for property private variables is very useful in avoiding situations where you may accidentally call the variable and not the property within the class itself. (Easily done with a simple lowercase intellisense input)

对属性私有变量使用下划线对于避免意外调用变量而不是类本身的属性的情况非常有用。(使用简单的小写智能输入即可轻松完成)

If you undertake some form of calculation or aggregation within the property get{} the last thing you want to do is miss this when your intention is to call the property in full.

如果您在属性 get{} 中进行某种形式的计算或聚合,当您打算完整调用属性时,您最不想做的就是错过它。