C# Default Access Modifier for Class Members – and drop that private habit!

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?


Actions

Information

10 responses to “C# Default Access Modifier for Class Members – and drop that private habit!”

27 05 2008
Matt Dunn (11:51:19) :

Nooooooo, but then I have to remember that no access modifier means private! And StyleCop will complain 🙂

Cheers,
Matt

19 06 2008
Tim (16:56:56) :

Glad to see you dont like writing those extra chars either.

11 03 2009
Jed Lawrence (03:43:29) :

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.

11 03 2009
Jed Lawrence (03:45:36) :

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.

3 04 2009
Mayur O (18:36:17) :

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.

24 06 2009
Stu (01:12:43) :

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!!

15 02 2010
Swapnil Lamkane (18:44:20) :

Jed Lawrence is right.

20 02 2010
Jhon Krammer (02:09:03) :

default access modifier for CLASS ?

29 03 2010
Veli (17:42:08) :

@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.

4 10 2010
Alex Khomich (15:52:57) :

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 🙂