如何最好的名字字段和属性 [英] How to best name fields and properties
问题描述
微软称字段和属性必须由不仅仅是情况更不同。 ?所以,如果他们真正代表了同样的想法应该怎么他们是不同的。
Microsoft says fields and properties must differ by more than just case. So, if they truly represent the same idea how should they be different?
这里有什么不能做微软的例子:
Here's Microsoft's example of what not to do:
using System;
namespace NamingLibrary
{
public class Foo // IdentifiersShouldDifferByMoreThanCase
{
protected string bar;
public string Bar
{
get { return bar; }
}
}
}
他们给上没有指导这应该怎么看。什么是大多数开发者怎么办?
They give no guidance on how this should look. What do most developers do?
推荐答案
没有,微软称的公开可见的成员的必须由超过不同刚刚情况:
No, Microsoft says publicly visible members must differ by more than just case:
这是公开可见的成员只有
This rule fires on publicly visible members only.
(这包括受保护的成员,他们是派生类是可见的。)
(That includes protected members, as they're visible to derived classes.)
因此,这是罚款:
public class Foo
{
private string bar;
public string Bar { get { return bar; } }
}
我个人的原则是,不要让任何其他私人领域无论如何,在这一点是没有问题的。
My personal rule is not to allow anything other private fields anyway, at which point it's not a problem.
你真的需要保护的领域?如何使财产有保护的制定者,如果你想能够从派生类变异呢?
Do you really need protected fields? How about making the property have a protected setter if you want to be able to mutate it from derived classes?
这篇关于如何最好的名字字段和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!