对于私有字段命名约定 [英] Naming convention for private fields

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

问题描述

首先,我知道这个问题已经被问了几次前,在最后,它主要是个人喜好的问题,而是阅读有关这个​​问题的所有线程,有些事情我也不清楚。

First, I know this question has been asked several times before and that in the end, it is mostly a matter of personal preference, but reading all the threads about the subject, some things are not clear to me.

基本上,一些大多数人的同意,至少是公共成员应PascalCased而私有成员应lowerCamelCased。

Basically, something that most people agree with at least is that public member should be PascalCased while private members should be lowerCamelCased.

这往往会带来争论这个问题是要不要用下划线,或其他任何私有成员前缀。前缀违反了若干规则StyleCop的(这显然可以关掉虽然)

The matter that usually brings debate is whether or not to prefix the private members by an underscore, or anything else. Prefixing violates several StyleCop rules (which can obviously be turned off though)

的理由不前缀是,你应该用这个。前缀来代替。

The rationale to not prefixing is that you should use this. to prefix instead.

我的问题是,我不明白它是如何发挥作用?
我的意思是,这是不喜欢你不是可以公开成员反正类内部使用。

The problem I have is that I don't understand how it is making a difference? I mean, it is not like you can't use this on a public member anyway inside a class.

让我们想象一下一类客户,这样看

Let's imagine a class Customer, looking like this:

class Customer
{
    private int age;

    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }
}



(很明显,在这样一个简单的情况下,我可以使用一个autoproperty,但是这只是一个例子)。

(Obviously, in such a simple case, I could use an autoproperty, but that's just an example).

如果我加入这个类中的第二个属性,没有什么能够阻止我使用this.Age来引用它(公共财产),而不是this.age(私有字段)。
有时,它甚至可以是wishable,如果一些验证或格式化的吸气水平的应用。

If I added a second property inside this class, nothing would prevent me to refer to it using this.Age (the public property) rather than this.age (the private field). Sometimes, it could even be wishable, if some validation or formatting was applied at the getter level.

另外,如果需要我的课的一些其他属性修改客户的时代,这将是有意义的直接使用属性,而不是支持字段作为二传手也可以实现一些业务规则验证,对吧?

Also, if some other properties of my class needed to modify the customer's Age, it would make sense to use the property rather than the backing field directly as the setter could also implement some business rules validations, right?

在换句话说,我实在看不出this关键字如何避免因为这个私人后盾成员和公共属性之间的混乱可以同时使用,智能感知同时显示?

In other words, I really don't see how the this keyword avoids the confusion between private backing members and public properties as this can be used on both and IntelliSense shows both?

感谢。

推荐答案

您说的很对。事实并非如此。

You are quite right. It doesn't.

使用这个是一种方式,以确保您使用的是类成员,在的情况下,命名冲突(比如一个参数名称是相同的字段名)。

Using this is a way to ensure you are using the class member, in case of naming conflicts (say a parameter name that is identical to a field name).

对于我来说,Pascal大小写公共成员和骆驼套私有成员一直是足够的公约工作得很好。

For me, pascal casing public members and camel casing private members has always been enough of a convention to work well.

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

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