VB.NET 私​​有字段的命名约定

发布于 2024-07-04 17:58:51 字数 326 浏览 6 评论 0 原文

VB.NET 中是否有命名私有字段的官方约定? 例如,如果我有一个名为“Foo”的属性,我通常将私有字段称为“_Foo”。 官方指南中似乎不赞成这种做法:

“不要使用例如,不要使用 g_ 或 s_ 来区分静态字段和非静态字段。”

在 C# 中,您可以调用私有字段“foo”、属性“Foo”,并在构造函数中将私有字段引用为“this.foo”。 由于 VB.NET 不区分大小写,因此您无法执行此操作 - 有什么建议吗?

Is there an official convention for naming private fields in VB.NET? For example, if I have a property called 'Foo', I normally call the private field '_Foo'. This seems to be frowned upon in the Offical Guidelines:

"Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields."

In C#, you could call the private field 'foo', the property 'Foo', and refer to the private field as 'this.foo' in the constructor. As VB.NET is case insensitive you can't do this - any suggestions?

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

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

发布评论

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

评论(10

半山落雨半山空 2024-07-11 17:58:51

我仍然在 VB 中对私有字段使用 _ 前缀,因此我将 _foo 作为私有字段,将 Foo 作为属性。 我也为 C# 以及我编写的几乎所有代码执行此操作。 一般来说,我不会太纠结于“什么是正确的方法”,因为并没有真正的“正确”的方法(尽管有一些非常糟糕的方法),而是关心始终如一地这样做。

最终,保持一致将使您的代码比使用任何一组“正确”约定更具可读性和可维护性。

I still use the _ prefix in VB for private fields, so I'll have _foo as the private field and Foo as the property. I do this for c# as well and pretty much any code I write. Generally I wouldn't get too caught up in "what is the right way to do it" because there isn't really a "right" way (altho there are some very bad ways) but rather be concerned with doing it consistently.

At the end of the day, being consistent will make your code much more readable and maintainable than using any set of "right" conventions.

ゃ懵逼小萝莉 2024-07-11 17:58:51

这是个人喜好,尽管人们普遍支持有一些区别。 即使在 C# 中,我也不认为存在一种广泛使用的约定。

杰夫·普罗西斯

根据个人喜好,我通常在私有字段前添加下划线[在 C# 中]...此约定在 .NET 框架中使用较多,但并未在整个框架中使用。

来自 .NET Framework 设计指南第二版第 73 页。

杰弗里·里希特

我将所有字段设为私有,并在实例字段前加上“m_”前缀,在静态字段前加上“s_”[在 C# 中]

来自 .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

As a matter of personal preference I typically prefix private fields with an underscore [in C#] ... This convention is used quite a lot in the .NET framework but it is not used throughout.

From the .NET Framework Design Guidelines 2nd Edition page 73.

Jeffrey Richter says

I make all my fields private and I prefix my instance fields with "m_" and my static fields with "s_" [in C#]

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.

凡间太子 2024-07-11 17:58:51

官方指导方针就是这样——指导方针。 你可以随时绕过他们。 话虽这么说,我们通常在 C# 和 VB.NET 中为字段添加下划线前缀。 这种约定很常见(显然,官方指南被忽略了)。

然后可以在不使用“me”关键字的情况下引用私有字段(“this”关键字用于 C# :)

Official guidelines are just that -- guidelines. You can always go around them. That being said we usually prefix fields with an underscore in both C# and VB.NET. This convention is quite common (and obviously, the Official Guidelines ignored).

Private fields can then be referenced without the "me" keyword (the "this" keyword is for C# :)

So要识趣 2024-07-11 17:58:51

您链接的设计指南明确指出它们仅适用于静态公共和受保护字段。 设计指南主要关注公共API的设计; 您对私人成员的处理方式取决于您。 我并不乐观,但我相对有信心,当编译器检查 CLS 合规性时,不会考虑私有成员,因为只有公共/受保护的成员才会在那里发挥作用(想法是,“如果有人使用以下语言怎么办?不允许 _ 字符尝试使用您的库?”如果成员是私有的,答案是“什么都没有,用户不必使用这些成员。”但如果成员是公共的,你就有麻烦了。 )

也就是说,我要补充一点,并指出无论你做什么,保持一致很重要。 我的雇主要求 C# 和 VB 中的私有字段都以 _ 为前缀,并且因为我们所有人都遵循此约定,所以很容易使用其他人编写的代码。

The design guidelines that you linked specifically state that they only apply to static public and protected fields. The design guidelines mostly focus on designing public APIs; what you do with your private members is up to you. I'm not positive but I'm relatively confident that private members are not considered when the compiler checks for CLS compliance, because only public/protected members come in to play there (the idea is, "What if someone who uses a language that doesn't allow the _ character tries to use your library?" If the members are private, the answer is "Nothing, the user doesn't have to use these members." but if the members are public you're in trouble.)

That said, I'm going to add to the echo chamber and point out that whatever you do, it's important to be consistent. My employer mandates that private fields in both C# and VB are prefixed with _, and because all of us follow this convention it is easy to use code written by someone else.

深海夜未眠 2024-07-11 17:58:51

在 VB.NET 4.0 中,大多数人可能知道不需要为属性声明显式编写 getter 和 setter,如下所示:

Public Property Foo As String
Public Property Foo2 As String

VB 自动创建名为 _Foo 和 _Foo2 的私有成员变量。 微软和 VS 团队似乎已经采用了 _ 约定,所以我不认为它有问题。

In VB.NET 4.0, most of you probably know you don't need to explicitly write getters and setters for your Property declarations as follows:

Public Property Foo As String
Public Property Foo2 As String

VB automatically creates private member variables called _Foo and _Foo2. It seems as though Microsoft and the VS team have adopted the _ convention, so I don't see an issue with it.

夜巴黎 2024-07-11 17:58:51

我不认为有正式的命名约定,但我看到 Microsoft 在 Microsoft.VisualBasic dll 中使用 m_ (通过反射器)。

I don't think there is an official naming convention, but i've seen that Microsoft use m_ in the Microsoft.VisualBasic dll (via reflector).

软甜啾 2024-07-11 17:58:51

我仍然在 VB 中使用 _ 前缀
私有字段,所以我将 _foo 作为
私有字段和 Foo 作为
财产。 我也为 c# 这样做
几乎我写的任何代码。
一般来说我不会太着迷
在“正确的做法是什么”中
因为没有真正的“权利”
方式(虽然有一些非常糟糕的
方式),而是关心
始终如一地这样做。

我还没有找到比“_”更好的澄清和一致性。 缺点包括:

我会通过在编辑器中关闭这些水平线来绕过这些线,并尽量不要过多考虑 CLS 合规性。

I still use the _ prefix in VB for
private fields, so I'll have _foo as
the private field and Foo as the
property. I do this for c# as well and
pretty much any code I write.
Generally I wouldn't get too caught up
in "what is the right way to do it"
because there isn't really a "right"
way (altho there are some very bad
ways) but rather be concerned with
doing it consistently.

I haven't found anything better than the "_" for clarify and consistency. Cons include:

I get around the lines by turning those off in the editor, and try not to think too much about the CLS compliance.

笑,眼淚并存 2024-07-11 17:58:51

我同意 @lomaxx 的观点,整个团队保持一致比拥有正确约定更重要。

不过,这里有几个获得编码约定的想法和指导的好地方:

  1. Microsoft Visual 的实用指南和最佳实践Francesco Balena 的《Basic and Visual C# Developers》是一本很好的书,解决了其中的许多问题。
  2. IDesign 编码标准(适用于 C# 和 WCF)
  3. .NET Framework 源代码(在VS2008中)

I agree with @lomaxx, it's more important to be consistent throughout the team than to have the right convention.

Still, here are several good places to get ideas and guidance for coding conventions:

  1. Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C# Developers by Francesco Balena is a great book that addresses many of these issues.
  2. IDesign Coding Standards (for C# and for WCF)
  3. The .NET Framework Source Code (in VS2008)
甚是思念 2024-07-11 17:58:51

我更喜欢对私有字段使用下划线前缀。 我使用小写的第一个字母作为方法参数。 我遵循为方法使用小写驼峰式参数的准则,我认为这比私有字段的命名更重要,因为它是类 API 的一部分。 。 例如,

Public Class Class1

    Private _foo As String
    Public Property Foo() As String
        Get
            Return _foo
        End Get
        Set(ByVal value As String)
            _foo = value
        End Set
    End Property

    Public Sub New(ByVal foo As String)
        _foo = foo
    End Sub

End Class

使用此模式,您将不会与 C# 或 VB.NET 中的私有字段和构造函数参数发生任何命名冲突。

I prefer to use the underscore prefix for private fields. I use lowercase first letter for the method parameters. I follow the guideline of having lowercase camelcase parameters for methods, which I regard as more important than the naming of private fields since it is part of the API for the class. . e.g.

Public Class Class1

    Private _foo As String
    Public Property Foo() As String
        Get
            Return _foo
        End Get
        Set(ByVal value As String)
            _foo = value
        End Set
    End Property

    Public Sub New(ByVal foo As String)
        _foo = foo
    End Sub

End Class

Using this pattern, you won't have any naming conflicts with the private field and your constructor parameter in C# or VB.NET.

咽泪装欢 2024-07-11 17:58:51

我同意最重要的不是一个人使用什么风格,而是它的一致性。

话虽如此,私有字段的新 MS/.NET 样式往往是 _fooVar(下划线后跟驼峰命名)

I agree most important is not what style one uses but it being consistent.

With that said, the new MS/.NET styling for private fields tends to be _fooVar (underscore followed by a camelCased name)

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