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.

What is the capitalization conventions for protected field names in C#?

Is it _myVar (like private field) or MyVar (like properties)?

share|improve this question
2  
Are you sure you want to have protected fields at all? Most of the reasons for not having public fields apply to protected ones too, so I think it's better to use protected properties instead. –  svick Nov 29 '12 at 8:38
    
in test cases, normally it is convenient to have protected fields.. but maybe you're right it can be changed to properties. But if I do want a protected field, what is the naming convention? –  Louis Rhys Nov 29 '12 at 8:42
1  
Are you sure _myVar is the right convention for private fields? (you are making a premise that it is) Not trying to start a religious war here, just trying to say that if you opted for some kind of arbitrary rule for private fields, you can pick another rule for your protected fields. The only advice is to be consistent. –  Marek Nov 29 '12 at 9:45
add comment

1 Answer

There are something that is called Design Guidelines for Developing Class Libraries which is written by Microsoft.

It says the following:

Do not provide instance fields that are public or protected.

Public and protected fields do not version well and are not protected by code access security demands. Instead of using publicly visible fields, use private fields and expose them through properties.

As well as:

  • Do use Pascal casing in field names

  • Do not use a prefix for field names. For example, do not use g_ or s_ to distinguish static versus non-static fields.

As for the _ prefix, it's used just as often as this this. convention (As far I've seen browsing source code in codeplex/github). ReSharper for instance promote _ in it's naming checks.

share|improve this answer
    
you have been a little selective in not including the "Do not use a prefix for field names." from MSDN alongside the Pascal casing rule :) While it is true that resharper by default promotes _ prefix, if you are quoting MSDN conventions IMHO you should not mix them with your own impressions. –  Marek Nov 29 '12 at 10:31
    
what does it mean "do not version well"? Are you saying that since protected field is not recommended, there is no guideline about its name? Anyway, Resharper seems to recommend MyVar –  Louis Rhys Nov 29 '12 at 10:40
1  
@Marek: I did include my own preference. I simply stated what Resharper prefer. And it was not my intention to be selective. Answer updated –  jgauffin Nov 29 '12 at 10:50
    
@LouisRhys: You can't change the handling of public/protected fields in future versions unless you want to break compatibility. With properties you can include logic in the property getters/setters in future versions. –  jgauffin Nov 29 '12 at 10:54
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.