什么是受保护字段的正确的符合 CLS 的命名约定?
我正在开发一个符合 CLS 的类型库,里面有一个类,其中包含私有、受保护和公共字段和属性。我使用下划线符号 (_) 作为私有或受保护字段的前缀,并使用小首字母来区分它们与具有相同名称的属性。看起来是这样的:
class SomeClass
{
private int _age; //Here is OK
public int Age { get { return this._get; } }
}
但是当我尝试使用受保护的字段时,我遇到了下一个问题:
class SomeClass
{
protected int _age; //Here is NOT CLS-compliant (because of _ can't be the first symbol of identifier)
public int Age { get { return this._get; } }
}
然后我尝试这样做:
class SomeClass
{
protected int age; //Here is NOT CLS-compliant (because of age and Age differ only in one symbol)
public int Age { get { return this._get; } }
}
请告诉我,对于这种情况,开发人员之间正确的 CLS 兼容符号或约定是什么?我必须使用像 l_age 这样的 C 风格前缀吗?
I am developing a CLS-compliant types library and I have a class inside It, which contains private, protected and public fields and properties. I use underscore symbol (_) as prefix for private or protected fields and little first letter to distinct their from properties with same names. It looks so:
class SomeClass
{
private int _age; //Here is OK
public int Age { get { return this._get; } }
}
But when I try to use protected fields I collide with a next problem:
class SomeClass
{
protected int _age; //Here is NOT CLS-compliant (because of _ can't be the first symbol of identifier)
public int Age { get { return this._get; } }
}
Then I tried to do in such way:
class SomeClass
{
protected int age; //Here is NOT CLS-compliant (because of age and Age differ only in one symbol)
public int Age { get { return this._get; } }
}
Please, tell me, what is right CLS-compliant notation or convention between developers for such cases? Have I to use prefixes in C-style like l_age?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个更正确的版本,IMO:
或者简单地说:
如果您正确封装它,那么该字段的名称并不重要,因为该类型之外的没有任何东西可以看到它。
在评论中,然后提出了事件的问题,例如:
这里我再次断言,没有理由保护该字段;该事件不属于派生类。它有 2 个需要执行的合理操作:
前者应该通过
On*
模式完成;后者应该只使用常规访问器(否则它会违反锁定)。另外,即使我们假设lock(this.StateChanged)
是一个拼写错误(这将是一个非常非常糟糕的事情,用作锁定对象 -it 根本不起作用),请注意,在 C# 4.0 中,编译器内置了更高效的锁定策略(使用Interlocked
而不是监视
)当你写一个“类似字段”事件(即没有明确的添加
/删除
)。因此,这里的首选方法是:并且......就是这样!
StateChanged +=
和StateChanged -=
OnStateChanged(...)
OnStateChanged
添加一个覆盖
,不需要任何非私有的字段。
Here's a more correct version, IMO:
or simply:
If you properly encapsulate it, then it doesn't matter what the field is called, as nothing outside that type can see it.
In comments, the question of events is then raised, with the example:
Here I again assert that there is no reason for this field to be protected; the event does not belong to the derived class. It has 2 sensible operations it can want to perform:
The former should be done via the
On*
pattern; the latter should just use the regular accessors (otherwise it violates the lock). Also, even if we assume thatlock(this.StateChanged)
is a typo (that would be a really, really bad thing to use as the lock object -it would not work at all), note that in C# 4.0 the compiler has a much more efficient lock strategy inbuilt (that usesInterlocked
rather thanMonitor
) when you write a "field-like" event (i.e. no explicitadd
/remove
). Consequently, the preferred approach here would be:and... that's it!
StateChanged +=
andStateChanged -=
OnStateChanged(...)
override
toOnStateChanged
no need for any non-private fields.
为了支持 Marc 的回答,Microsoft 的字段设计指南指出:
这可能就是为什么您找不到任何有关命名它们的有用指导(事实上,命名指南只需返回“字段设计”页面即可)。
To back up Marc's answer, the guidance on Field Design from Microsoft states:
This is probably why you will not find any useful guidance on naming them (in fact, the naming guidelines just point back at the Field Design page).