VB.NET专用字段的命名约定 [英] Naming convention for VB.NET private fields

查看:114
本文介绍了VB.NET专用字段的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中是否有用于命名私有字段的官方约定?例如,如果我有一个名为 Foo的属性,则通常将其命名为 _Foo。 官方准则



不要在字段名中使用前缀。例如,不要使用g_或s_来区分静态字段和非静态字段。



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

解决方案

我仍然在VB中使用_前缀对于私有字段,因此我将_foo作为私有字段,并将Foo作为属性。我也为c#和几乎所有我编写的代码执行此操作。通常,我不会太着迷于什么是正确的方法,因为实际上并没有真正的正确方法(尽管有一些非常糟糕的方法),而是要始终如一地关心它。 / p>

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


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?

解决方案

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.

这篇关于VB.NET专用字段的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆