C#需要命名建议
我使用_varName作为私有变量 这是为了区分私有变量和构造函数参数C#需要命名建议,c#,naming-conventions,C#,Naming Conventions,我使用_varName作为私有变量 这是为了区分私有变量和构造函数参数 想要公开这段代码,看看是否有更好的方法来区分私有变量和方法参数如何this.variable=variable?有不同的可能性 您可以使用: public class SomeClass { private readonly AuthenticationService _authenticationService = null; private readonly ProfileService _profi
想要公开这段代码,看看是否有更好的方法来区分私有变量和方法参数如何
this.variable=variable代码>?有不同的可能性
您可以使用:
public class SomeClass
{
private readonly AuthenticationService _authenticationService = null;
private readonly ProfileService _profileService = null;
public SomeClass(IAuthenticationService authenticationService, ProfileService profileService)
{
_authenticationService = authenticationService;
_bgMeterProfileService = bgMeterProfileService;
}
...
我个人会使用第一个版本,并且在针对当前实例成员时总是使用“this.
”
最重要的是,在代码中的任何地方都使用相同的约定。区分私人项目和公共项目很重要
有些人使用前导下划线,有些人使用匈牙利符号,例如m_intMyIntVar或m_strMyStringVar
其中m_u表示成员,下一位是类型缩写,然后是名称
主要是因为个人喜好。我一直在使用\u varName=varName代码>-过去几年的惯例,并将在未来几年继续使用它,至少在大多数可发现性(智能感知)、调试/可读性(避免与本地变量的歧义)方面最方便。上述建议(2.6命名)。将此标记为已接受的应答。感谢+1使用此.VarName。与官方框架设计指南保持一致
this.variable = variable;
this._variable = variable;
this.m_variable = variable;