C# 中类字段的命名首选项?
我见过 C# 中用于字段的几种命名约定。 它们是:
下划线
public class Foo
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
这个
public class Foo
{
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
成员前缀
public class Foo
{
private string m_name;
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
您更喜欢哪一个? 您有更喜欢的其他方式吗? 只是想知道其他人的感受是最佳实践。
I have seen several naming conventions used for fields in C#. They are:
Underscore
public class Foo
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
This
public class Foo
{
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
Member Prefix
public class Foo
{
private string m_name;
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
Which do you prefer? Is there a different way you prefer to do it? Just curious to know what others feel is a best practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我总是使用你所说的this风格,尽管我不会虔诚地使用
this。
I always use what you call the this style, though I won't religiously use
this.
我通常使用:
只是删除了这个。 因为它相当多余
I usually use:
just removed the this. since it's rather redundant
最佳实践是选择一个,并保持一致。
我个人更喜欢“”。 在我看来,“m”总是让“成员身份”看起来很特别,就好像它是为了来自不存在“成员”背景的开发人员的利益。 与“这个”有点相同。 我更喜欢只用“名字”。
The best practice is to pick one, and be consistent.
I prefer "", personally. "m" has always seemed to me to make the "memberness" seem to be something special, as though it were for the benefit of developers coming from a background where no "members" exist. Sort of the same with "this.". I'd prefer just "name" instead.
1)这是一个纯粹的风格/主观问题,因此一般来说,其中一个与另一个一样好。 (我有这样的资格,因为上次我回答这样的问题时,即使有几乎相同的资格,我还是有各种各样的人投票反对我并告诉我我错了。)
2)我使用
this方法。 这是 StyleCop 使用的默认技术,目前我没有任何其他可用的源分析工具。 这是完全可以接受的。
1) This is a purely stylistic/subjective question, so one of these is, in general, as good as another. (I qualify that because the last time I answered a question like this, even with a nearly identical qualification, I had all kinds of people voting me down and telling me I was wrong.)
2) I use the
this
method. It's the default technique used by StyleCop, and I don't have any other source analysis tool available to me at the moment. It's perfectly acceptable.