The default access modifier for the members of a C# class (eg, fields, methods, and properties) is ‘private’. As such, I recommend never using the redundant ‘private’ keyword for class members. Leaving off the private nicely separates your privates from your public/inheritable interface in syntax highlighting. It also saves people having to read redundant code – you wouldn’t want your code to be full of un-needed casts, or redundant ‘this.’ references, would you?
Matt Dunn
Nooooooo, but then I have to remember that no access modifier means private! And StyleCop will complain 🙂
Cheers,
Matt
Tim
Glad to see you dont like writing those extra chars either.
Jed Lawrence
I disagree. As the “owner” of the C# and .NET standards document for several organizations, we make it a violation of our standards is any programmer creates any class, struct, enum or interface or any member for same without an access modifier. The inclusion of the access modifier makes it explicit what the programmer’s intentions were which is a fundamental goal of creating standards. When a programmer leaves off an access modifier we do not know whether they did not know what they were doing and they were just lucky that it worked or whether they were assuming that since thy know what the “default” is that they are assuming that ALL .NET programmers know what the default is (an absolutely, horrendous assumption). Good programming standards call for always including the access modifier. Most automated code review environments expect the access modifiers.
Jed Lawrence
I disagree. As the “owner” of the C# and .NET standards document for several organizations, we make it a violation of our standards if any programmer creates any class, struct, enum or interface or any member for same without an access modifier. The inclusion of the access modifier makes it explicit what the programmer’s intentions were which is a fundamental goal of creating standards. When a programmer leaves off an access modifier we do not know whether they did not know what they were doing and they were just lucky that it worked or whether they were assuming that since they know what the “default” is that they are assuming that ALL .NET programmers know what the default is (an absolutely, horrendous assumption). Good programming standards call for always including the access modifier. Most automated code review environments expect the access modifiers.
Mayur O
Jed I would have to disagree here with you – depending on how much code you have the compiler may get slower and slower if add the default access modifiers in the code.
Stu
Well I’m agreeing with Jed because I’ve been developing for years and to be honest forgot what the default was which led me to this post. Go standards go!!
Swapnil Lamkane
Jed Lawrence is right.
Jhon Krammer
default access modifier for CLASS ?
Veli
@Jhon
The default access modifier for classes and interfaces is internal.
I also totally agree with Jed Lawrence. Explicit access modifiers are always a good coding practice, easy on the compiler and easy on the eyes when going through the code.
Alex Khomich
Agree with author. But personally I’ve an issue with it because I’m using Java 1/2 of my working time. I skipping to set private in Java too and all of my fields become package protected… Not good 🙂