我一般不会对变量命名、代码格式等问题进行主观争论。所以我无意在这里展开争论。
我刚刚遇到这篇(旧)博客文章建议不要为成员变量名称添加前缀:
请勿不要使用成员前缀
变量(_、m_、s_ 等)。如果你
想要区分本地和
您应该使用的成员变量
“这。”在 C# 和“我”中。在 VB.NET 中。
对于 C#,是的,我明白了:成员变量可以采用小驼峰命名法,公共属性/方法可以采用 PascalCase 命名法。但 VB.NET 不区分大小写,因此您不能真正为私有成员指定与公共属性相同的名称,除非首字母小写。
我通常在成员变量前加上下划线,但有人告诉我这不是惯用的。
所以我真的很好奇:如何在 VB.NET 中命名成员变量?有没有“标准”的方式?
我问这个问题并不是因为我相信有一个“正确”的方法,或者因为我特别想改变我的风格,当然也不是因为我想告诉别人他们是“错的”。就像我说的,我只是好奇。
I am generally not one to engage in subjective arguments over matters like variable naming, code formatting, etc. So I have no intention of starting an argument here.
I just came across this (old) blog post which recommends not prefixing member variable names:
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.
For C#, yeah, I get it: member variables can be lower camelCase, and public properties/methods can be PascalCase. But VB.NET is case-insensitive, so you can't really give a private member the same name as a public property except with a lower case first letter.
I've generally prefixed member variables with an underscore, but I've been told that's not idiomatic.
So really I'm just curious: how do you name your member variables in VB.NET? And is there a "standard" way?
I'm not asking because I believe there's a "right" way or because I particularly want to change my style, and certainly not because I have any desire to tell others they're "wrong." Like I said, I'm just curious.
发布评论
评论(7)
这是个人喜好,尽管人们普遍支持有一些区别。即使在 C# 中,我也不认为存在一种广泛使用的约定。
杰夫·普罗西斯 说
来自 .NET Framework 设计指南第二版第 73 页。
杰弗里·里希特说
来自 .NET Framework 设计指南 第二版第 47 页。 Anthony Moore (BCL 团队) 也认为使用“m_”和“s_”值得考虑,第 48 页。
It's personal preference, although there's widespread support for having some distinction. Even in C# I don't think there's one widely used convention.
Jeff Prosise says
From the .NET Framework Design Guidelines 2nd Edition page 73.
Jeffrey Richter says
From the .NET Framework Design Guidelines 2nd Edition page 47. Anthony Moore (BCL team) also thinks using "m_" and "s_" is worth consideration, page 48.
我正在像你一样这样做。
在构造函数中
但我认为这是一个品味问题。
I'm doing it like you.
And in constructor
But I think that's a matter of taste.
我个人使用 m_ 作为成员变量。
尽管 VS 2010 中具有自动属性,但我最近编写的任何新代码都不需要这样做。
I personally use m_ for member variables.
Although with automatic properties in VS 2010 I haven't needed to for any new code I've written recently.
我不喜欢用下划线开始行/名称,因为这总是看起来好像该行被额外的空格缩进了:它只会使代码不平衡。此外,对于我的口味来说,单独的下划线太不显眼了:我更喜欢标识符清晰可见。
因此,我定期在后缀下划线(例如
example_
)和前缀m_
之间循环。我无法决定我更喜欢哪一个,因为我实际上都不喜欢。但反对前缀下划线的论点部分也适用于后缀下划线。但正如您所说,某种的区别是必要的。
正如我在其他地方说过的,我在 C# 中仅区分大小写方面也有过非常糟糕的经历 - 很容易混淆名称,因此写入私有变量而不是属性。如果属性检查或转换设置值,这一点很重要。
因此,我也更喜欢在 C# 中使用某种前缀。
I don’t like starting a line/name with an underscore since that always looks as if the line were indented by an additional space: it just makes the code unbalanced. Additionally, a lonely underscore is too inconspicuous for my taste: I prefer the identifiers to be clearly distinct.
Therefore, I periodically cycle between suffix underscore (e.g.
example_
) and prefixm_
. I can’t decide which of those I prefer since I actually like neither. But the argument against prefix underscores partially also applies to suffix underscores.But as you’ve remarked, some kind of distinction is necessary.
And as I’ve remarked elsewhere, I’ve had very bad experiences with case-only distinction in C# as well – it’s just too easy to confuse the names, and hence write into a private variable instead of the property. This matters if the property either checks or transforms the set value.
For that reason, I prefer to use some kind of prefix in C# as well.
我唯一一次使用前缀是在公共属性的私有后备存储中。在这些情况下,名称在其他方面是相同的,并且大多数情况下,我唯一引用前缀名称的地方是在其关联的属性内。当我最终可以使用 VB.Net 自动实现的属性时,我什至不需要这样做。
当我不能只使用自动实现的属性时,我也会在 C# 中执行此操作。 _ 前缀比仅根据大小写改变名称更好。
The only time I use a prefix is with the private backing store for a public property. In these cases, the names are otherwise identical and most of the time the only place I'll ever reference the prefixed name is inside it's associated property. When I can finally use auto-implemented properties with VB.Net I won't even need to do that.
I do this in C# as well, on those instances when I can't just use an auto-implemented property. Better the _ prefix than varying the names only by case.
我们使用 _(下划线)作为变量名称的前缀。它很短而且切中要点...
We use _ (underscore) to prefix our variables names. It's short and to the point...
虽然很多 MS 代码似乎使用 m_* 进行私有声明,但我为自己保存了一个字符,只使用 _name 作为私有成员。我的规则:
由于我使用 C# 工作,因此参数名称与大小写不同的属性名称相同是没有问题的。但这在 VB 中行不通。
Although a lot of the MS code seems to use m_* for private declarations, I save myself a character and just use _name for private members. My rules:
Since I work in C#, having a parameter name with the same name as a property with different case is no problem. That won't work in VB, though.