如果私有变量已经是私有的,我应该将它们标记为私有吗?
据我所知,在 C# 中,如果没有另外标记,所有字段默认都是私有的。
class Foo
{
private string bar;
}
class Foo
{
string bar;
}
我想这两个声明是相等的。
所以我的问题是:如果私有变量已经是私有的,我应该将它们标记为私有变量吗?
As far as I know, in C# all fields are private for default, if not marked otherwise.
class Foo
{
private string bar;
}
class Foo
{
string bar;
}
I guess these two declarations are equal.
So my question is: what for should I mark private variables as private
if they already are private?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我认为为了可读性,最好是明确的。
另外,您可能希望看一下名为 Code Style Enforcer 的 Visual Studio 插件 (http://joel.fjorden.se/static.php?page=CodeStyleEnforcer),它使用 dxCore 扩展来提供有关您的代码是否符合编码标准的实时反馈(完全可定制)。
I think for readability it is always best to be explicit.
As a side, You may wish to have a look at a visual studio plug-in called Code Style Enforcer (http://joel.fjorden.se/static.php?page=CodeStyleEnforcer) which uses the dxCore extensions to provide real-time feedback on your codes adherence to coding standards (fully customisable).
我个人更喜欢明确标记默认私有和默认公共字段。 您可能很了解默认设置,但每当您快速扫描代码时,您的大脑都会喜欢冗长的内容。
I personally prefer marking the default private and default public fields explicitly. You may well know the defaults but your brain will like verbosity whenever you quickly scan the code.
我对此已经持观望态度有一段时间了。 我曾经主张将其隐式化,但现在我认为我倾向于将其明确化。
将其保留为隐式的原因:
使其显式的原因:
后面的几点是 基本上是由埃里克·利珀特(Eric Lippert) 当我们不久前讨论过这个问题时。
I've been on the fence for a while about this. I used to argue for leaving it implicit, but now I think I'm tipped over towards making it explicit.
Reasons for leaving it implicit:
Reasons for making it explicit:
These latter points are basically the ones made by Eric Lippert when we discussed it a while ago.
是的,它们是相等的,但我喜欢将私有变量标记为私有,我认为这可以提高阅读能力。
我也对私有成员使用这个通用符号,它非常有用:
Yes they are equal but I like to mark private variables as private, I think it improves reading.
also I use this common notation for private members, it's very useful :
这纯粹是一个编码标准问题,但无论其价值如何,我总是明确地将私有成员标记为私有。
This is purely a coding standards question but, for what it's worth, I always explicitly mark private members as private.
如果您定期在 Java 和 C# 之间切换,我想明确指定访问修饰符将相当重要。 例如,在 Java 中,
包中的任何类都可以访问该方法。 在 C# 中,它显然是类和内部类私有的。
If you were switching between Java and C# on a regular basis I imagine it would be fairly important to specify the access modifier explicity. For example in Java
any class in your package has access to that method. In C# it's obviously private to the class and inner classes.
不要让人们猜测,不要让他们做出错误的假设,也不要认为更少的字符就等于清晰。
没有充分的理由不明确这一点,恕我直言,C# 支持它是一个错误(特别是如果他们愿意出于同样的原因做他们所做的事情来切换语句)
Don't make people guess, don't let them make false assumptions, and don't think fewer characters in any way equates to clarity.
There's no good reason not to make this explicit, and imho it's a mistake for C# to support it (especially if they're willing to do what they did to switch statements for the same reason)
显式使用 private 可以提高某些边缘情况下的可读性。
示例:
查看此片段,您可能不确定您是在方法范围内还是在类范围内。
在字段名称中使用
private
或下划线(private int spam;
、int spam_;
或int _spam;
)将消除混乱。Explicitly using private can improve readability in certain edge cases.
Example:
Looking at this fragment you may be unsure whether you're in method or class scope.
Using either
private
or underscore in field name (private int spam;
,int spam_;
orint _spam;
) will eliminate the confusion.由你决定。 做最有利于可读性或对您的情况有意义的事情。 我将它们标记为私有只是为了清楚起见。
Up to you. Do what's best for readability or makes sense in your case. I mark them as private just to make it clear though.
现在; 无论如何,字段应该几乎始终是私有的,因此您是否应该打扰是一个边缘情况。
对于更广泛的主题,我记得 Eric Lippert 的评论 - 本质上是说给定一个方法/类/无论什么:
然后不清楚它们是否故意是私有/内部的,或者开发人员是否考虑过它,并且决定它们应该是私有的/内部的/无论什么。 所以他的建议是:告诉读者你是故意做事而不是偶然——把它说清楚。
Now; fields should pretty-much always be private anyway, so it is an edge case whether you should bother.
For the wider subject, I remember a comment by Eric Lippert - essentially saying that given a method / class / whatever:
Then it isn't clear whether they are private/internal deliberately, or whether the developer has thought about it, and decided that they should be private/internal/whatever. So his suggestion was: tell the reader that you are doing things deliberately instead of by accident - make it explicit.