需要c#命名建议

本文关键字:需要 | 更新日期: 2023-09-27 18:07:09

 public class SomeClass
 {
    private readonly AuthenticationService _authenticationService = null;
    private readonly ProfileService _profileService = null;
    public SomeClass(IAuthenticationService authenticationService, ProfileService profileService)
    {
        _authenticationService = authenticationService;
        _bgMeterProfileService = bgMeterProfileService;
    }
   ...

我使用_varName作为私有变量

这是为了区分私有变量和构造函数参数。

想把这段代码公开,看看是否有更好的方法来区分私有变量和方法参数

需要c#命名建议

this.variable = variable;呢?

有不同的可能性

你可以使用:

this.variable = variable;
this._variable = variable;
this.m_variable = variable;

我个人会使用第一个版本,并且在针对当前实例成员时总是使用"this."。

最重要的是你总是在代码的任何地方使用相同的约定

我在过去几年里一直在使用_varName = varName; -convention,并将在接下来的几年中继续使用它,至少在大多数可发现性(智能感知),调试/可读性(避免与本地变量混淆)方面最方便。

区分私有项目和公共项目是很重要的。

有些人使用前导下划线,有些人使用匈牙利符号,例如m_intMyIntVar或m_strMyStringVar

其中m_表示成员,下一位是类型缩写,然后是名称。