Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
Why explicitly write "private"? –  Cody Gray May 24 '12 at 11:22
add comment

2 Answers

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.

share|improve this answer
    
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. –  Lucifer Sam May 23 '12 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 '12 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 '12 at 17:22
add comment

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.

share|improve this answer
1  
Do you always implement interfaces in C# explicitly then? –  Den May 23 '12 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 '12 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 '12 at 18:13
    
@Den that's a bad example because you shouldn't normally implement interfaces explicitly. See Implementing Interface Members Explicitly –  Conrad Frix May 23 '12 at 20:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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