C# 和 VB Class 私有变量命名实践和“_”字符
C# and VB Class Private Variable Naming Practices and the "_" Character
Let's be clear: I'm not looking for an opinion, but an answer, an actual documented source or reference if possible. (Preferably something from MSDN?)
在 .NET 中创建 class 时,我遵循的基本结构如下:
- 正在创建私有变量字段。
- 使用 get/set 定义 class 属性。
- 正在创建构造函数。
- 为数据库功能添加额外的 class 方法。
- 杂项功能。
我过去是如何命名我的私有变量的:
private string _myString;
我目前如何命名我的私有变量:
private string iMyString;
我听说在私有变量声明中使用“_”字符会产生负面影响。然后我改变了我的做法,使用 "i" 字符。我发现对这些声明使用相同的字符有助于提高创建 class 属性的速度和总体开发时间。
我的问题是: class私有变量命名结构的行业标准是什么?并且,为什么使用“_”字符被认为是一种不好的做法?
我进行了相当多的 google 搜索,甚至在 MSDN 网站上,我查看的页面的示例也会定期更改,因此不一致。
我不确定这是否被认为是一个问题,但我倾向于练习质量代码。
来自.NET foundation coding style guidelines:
We use _camelCase
for internal and private fields and use readonly
where possible. Prefix instance fields with _
, static fields with s_
and thread static fields with t_
.
Let's be clear: I'm not looking for an opinion, but an answer, an actual documented source or reference if possible. (Preferably something from MSDN?)
在 .NET 中创建 class 时,我遵循的基本结构如下:
- 正在创建私有变量字段。
- 使用 get/set 定义 class 属性。
- 正在创建构造函数。
- 为数据库功能添加额外的 class 方法。
- 杂项功能。
我过去是如何命名我的私有变量的:
private string _myString;
我目前如何命名我的私有变量:
private string iMyString;
我听说在私有变量声明中使用“_”字符会产生负面影响。然后我改变了我的做法,使用 "i" 字符。我发现对这些声明使用相同的字符有助于提高创建 class 属性的速度和总体开发时间。
我的问题是: class私有变量命名结构的行业标准是什么?并且,为什么使用“_”字符被认为是一种不好的做法?
我进行了相当多的 google 搜索,甚至在 MSDN 网站上,我查看的页面的示例也会定期更改,因此不一致。
我不确定这是否被认为是一个问题,但我倾向于练习质量代码。
来自.NET foundation coding style guidelines:
We use
_camelCase
for internal and private fields and usereadonly
where possible. Prefix instance fields with_
, static fields withs_
and thread static fields witht_
.