如果私有变量已经是私有的,我应该将它们标记为私有吗?

发布于 2024-07-13 12:55:02 字数 201 浏览 8 评论 0原文

据我所知,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

々眼睛长脚气 2024-07-20 12:55:04

我认为为了可读性,最好是明确的。

另外,您可能希望看一下名为 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).

活泼老夫 2024-07-20 12:55:04

我个人更喜欢明确标记默认私有和默认公共字段。 您可能很了解默认设置,但每当您快速扫描代码时,您的大脑都会喜欢冗长的内容。

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.

全部不再 2024-07-20 12:55:03

我对此已经持观望态度有一段时间了。 我曾经主张将其隐式化,但现在我认为我倾向于将其明确化。

将其保留为隐式的原因:

  • 这意味着非私有成员(或比默认访问权限更多的任何成员)存在更大的差异; 这突出了阅读代码时的差异

使其显式的原因:

  • 一些开发人员可能不知道默认值:使其显式意味着每个人都清楚
  • 它表明您已经积极地做出了决定,而不是仅仅将其保留为默认值。

后面的几点是 基本上是由埃里克·利珀特(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:

  • It means there's a bigger difference for non-private members (or anything with more access than the default); this highlights the difference when reading the code

Reasons for making it explicit:

  • Some developers may not know the defaults: making it explicit means it's clear to everyone
  • It shows you've actively made a decision, rather than just leaving it up to the default

These latter points are basically the ones made by Eric Lippert when we discussed it a while ago.

德意的啸 2024-07-20 12:55:03

是的,它们是相等的,但我喜欢将私有变量标记为私有,我认为这可以提高阅读能力。

我也对私有成员使用这个通用符号,它非常有用:

private string _bar;

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 :

private string _bar;
天煞孤星 2024-07-20 12:55:03

这纯粹是一个编码标准问题,但无论其价值如何,我总是明确地将私有成员标记为私有。

This is purely a coding standards question but, for what it's worth, I always explicitly mark private members as private.

z祗昰~ 2024-07-20 12:55:03

如果您定期在 Java 和 C# 之间切换,我想明确指定访问修饰符将相当重要。 例如,在 Java 中,

void myMethod()
{

}

包中的任何类都可以访问该方法。 在 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

void myMethod()
{

}

any class in your package has access to that method. In C# it's obviously private to the class and inner classes.

满身野味 2024-07-20 12:55:03

不要让人们猜测,不要让他们做出错误的假设,也不要认为更少的字符就等于清晰。

没有充分的理由明确这一点,恕我直言,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)

∝单色的世界 2024-07-20 12:55:03

显式使用 private 可以提高某些边缘情况下的可读性。

示例:

        /*
        Tomorrow when we wake up from bed,
        first me and Daddy and Mommy, you, eat
        breakfast eat breakfast like we usually do,
        and then we're going to play and
        then soon as Daddy comes, Carl's going
        to come over, and then we're going to
        play a little while. And then Carl and
        Emily are both going down to the car
        with somebody, and we're going to ride
        to nursery school [whispered], and then
        when we get there, we're all going
        to get out of the car...
        */

        int spam;

        /*
        Does this style look at all familiar?
        It should!
        */

查看此片段,您可能不确定您是在方法范围内还是在类范围内。

在字段名称中使用 private 或下划线(private int spam;int spam_;int _spam;)将消除混乱。

Explicitly using private can improve readability in certain edge cases.

Example:

        /*
        Tomorrow when we wake up from bed,
        first me and Daddy and Mommy, you, eat
        breakfast eat breakfast like we usually do,
        and then we're going to play and
        then soon as Daddy comes, Carl's going
        to come over, and then we're going to
        play a little while. And then Carl and
        Emily are both going down to the car
        with somebody, and we're going to ride
        to nursery school [whispered], and then
        when we get there, we're all going
        to get out of the car...
        */

        int spam;

        /*
        Does this style look at all familiar?
        It should!
        */

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_; or int _spam;) will eliminate the confusion.

﹂绝世的画 2024-07-20 12:55:03

由你决定。 做最有利于可读性或对您的情况有意义的事情。 我将它们标记为私有只是为了清楚起见。

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.

若言繁花未落 2024-07-20 12:55:02

现在; 无论如何,字段应该几乎始终是私有的,因此您是否应该打扰是一个边缘情况。

对于更广泛的主题,我记得 Eric Lippert 的评论 - 本质上是说给定一个方法/类/无论什么:

void Foo() {}
class Bar {}

然后不清楚它们是否故意是私有/内部的,或者开发人员是否考虑过它,并且决定它们应该是私有的/内部的/无论什么。 所以他的建议是:告诉读者你是故意做事而不是偶然——把它说清楚。

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:

void Foo() {}
class Bar {}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文