2013-04-03 126 views
0
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#命名建议需要

这是私有变量和构造参数之间进行区分。

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

回答

7

如何this.variable = variable;

+1

http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx 上述建议(2.6命名)也是如此。 将此标记为已接受的答案。感谢 –

2

有不同的可能性。

你可以使用:

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

我会personnaly使用的第一个版本,并始终使用“this.”针对当前实例成员时。

什么是最重要的是,你总是在你的代码中使用相同的约定

+1

+1这个.VarName用法。与官方框架设计指南一致 –

0

区分私人和公共物品很重要。

有些人使用领先的下划线,有些人使用匈牙利语法,例如m_intMyIntVar或m_strMyStringVar

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

大多数情况下这取决于个人偏好。

1

我在过去几年中一直使用_varName = varName;-convention,并将继续用于接下来的几个版本,因为在大多数可发现性(intellisense),调试/可读性(避免本地变量的歧义) 。