以 _ 开头的受保护变量和 CLSCompliant 属性的含义
我们有一些 C# 代码,其中有用下划线命名的受保护变量,
protected string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
这会生成 CLS 合规性警告,因为 CLS 不喜欢开头的下划线。
用 [CLSCompliant(false)] 标记受保护变量有什么含义?我什至不知道用什么语言来测试是有问题的。如果 _name 根本无法访问,这对于我们的目的来说是没问题的,但如果它导致命名歧义,那就不行了。
We have some C# code where there are protected variables that have been named with underscores
protected string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
This generates CLS compliance warnings, as CLS does not like underscores at the beginning.
What are the implications of marking the protected variable with [CLSCompliant(false)]? I don't even know what languages this is an issue with to test things. If _name is simply inaccessible this is fine for our purposes, but if it causes naming ambiguity it is not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
某些语言不支持以下划线开头的变量。如果使用其中一种语言的客户端想要从您的类继承,他将无法访问
_name
字段。这些是什么语言,我不知道。从设计的角度来看,我想知道为什么当公共(而不是虚拟)属性允许直接获取和设置时,您会有一个受保护的支持字段。有什么意义?在这种情况下,授予继承者对支持字段的访问权限不会带来任何好处,并且无法更改
Name
属性的实现。Some languages don't support variables that start with an underscore. If a client using one of those languages wants to inherit from your class, he won't be able to access the
_name
field. What languages those are, I don't know.From a design perspective, I'm wondering why you'd have a protected backing field when the public (and not virtual) property allows get and set directly. What's the point? In this case, giving inheritors access to the backing field provides no benefit and makes it impossible to change the implementation of the
Name
property.