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?
This is purely a coding standards question but, for what it's worth, I always explicitly mark private members as private.
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.
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.
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)
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.
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.