4

From MSDN...

The access level for class members and struct members, including nested classes and structs, is private by default.

If class members are private by default then why use the private access modifier for them? I see it all the time in code examples and open source projects including the ASP.NET MVC source. I even use it in my own projects but I'm left wondering why.

1

2 Answers 2

16

Making them private explicitly makes the code more clear, since now you don't have to depend on developers knowing the default visibility in C#. This could thrown some developers off, so it's better to just be explicit and clear. For example, the default visibility in Java is package, not private, so this might throw off ex-Java programmers.

3
  • 6
    Well you've got the votes, but I'm still not sure I'm convinced. Every language has a different syntax. It seems insane to worry about "throwing off" another coder who is not familiar with the language. Should we never use var, collection initializers or the null-coalescing operator as well? The C# standard could have required access modifiers for class members but it doesn't. May 23, 2012 at 16:36
  • @LuciferSam - Feel free not to be convinced. It should be pointed out var is a very specfic type anonymous what it means is crystal clear. What type it will be is up to the compiler. Of course using var for everything all the time is not a good idea either.
    – Ramhound
    May 23, 2012 at 17:06
  • @LuciferSam At the core of it, you are removing the amount of language knowledge you need to have to read some piece of code, and this is a good thing. You make it explicit so that there is no doubt about the visibility. With things like var, the developer almost has look it up to proceed with the code, but with omitting data like private, a lot of developers might miss the implicit meaning you are giving the code.
    – Oleksi
    May 23, 2012 at 17:22
8

Because explicitness should be favored over implicitness.

Always writing it also makes you think about what visibility the member should be, leading to fewer bugs due to overlooking it.

4
  • 2
    Do you always implement interfaces in C# explicitly then?
    – Den
    May 23, 2012 at 15:47
  • @Den: Often times the coding standard I'm working under requires I do not. I personally tend to implement interfaces explicitly when not so constrained.
    – Telastyn
    May 23, 2012 at 15:52
  • @Den he said favoured that implies some flexibility. We do (or should do) a number of things not because they are necessary in every case but because the habit of so doing reduces the chances of accidents in some cases
    – Murph
    May 23, 2012 at 18:13
  • @Den that's a bad example because you shouldn't normally implement interfaces explicitly. See Implementing Interface Members Explicitly May 23, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.