James Crisp

Software dev, tech, mind hacks and the occasional personal bit

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?

Previous

WPF Control Inheritance With Generics

Next

Bounded Actions Using Lambda – IDisposable is old and ugly!

10 Comments

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

    Cheers,
    Matt

  2. Tim

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

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

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

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

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

  7. Swapnil Lamkane

    Jed Lawrence is right.

  8. default access modifier for CLASS ?

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

  10. 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 🙂

Comments are closed.

Powered by WordPress & Theme by Anders Norén