What for should I mark private variables as privat

2020-03-06 02:31发布

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?

10条回答
手持菜刀,她持情操
2楼-- · 2020-03-06 03:07

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

查看更多
手持菜刀,她持情操
3楼-- · 2020-03-06 03:08

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.

查看更多
一纸荒年 Trace。
4楼-- · 2020-03-06 03:21

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.

查看更多
Anthone
5楼-- · 2020-03-06 03:23

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)

查看更多
迷人小祖宗
6楼-- · 2020-03-06 03:26

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.

查看更多
来,给爷笑一个
7楼-- · 2020-03-06 03:29

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.

查看更多
登录 后发表回答