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

发布于 2024-07-13 14:41:29 字数 95 浏览 4 评论 0原文

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

这是什么意思? 这有什么具体的含义吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏九 2024-07-20 14:41:29

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

就我个人而言,我不使用这样的前缀,但这是一种风格选择。 只要从事同一个项目的每个人都保持一致,通常就不是什么大问题。 不过,我见过一些非常不一致的代码......

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.

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 horribly inconsistent code though...

℉服软 2024-07-20 14:41:29

一般来说,这意味着私有成员字段。

In general, this means private member fields.

雨后彩虹 2024-07-20 14:41:29

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

我将补充 @Steven Robbins 所说的内容:

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

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

I'll add to what @Steven Robbins said:

private string _val;
public string Values
{
    get { return _val;}
    set {_val = value;}
}
静水深流 2024-07-20 14:41:29

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

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

岁月苍老的讽刺 2024-07-20 14:41:29

以“”开头的实例属性还有另一个优点,它首先显示在 Intellisense 上。
创建值/模型/POCO 类时,我不使用“
”。

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 '
'.

不弃不离 2024-07-20 14:41:29

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

您和您的团队可以决定是否遵循这些命名约定,只要你们所有人都知道这一决定。 我也将它们与非公共字段(例如私有字段或受保护字段)一起使用,只是为了识别它们。

这里有一点如果您想进一步阅读,请简要介绍一下

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.

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.

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

梦冥 2024-07-20 14:41:29

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

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

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.

看春风乍起 2024-07-20 14:41:29

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文