17

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

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

  • 3
    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
  • 2
    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
13

There is 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 the this. convention (as far I've seen browsing source code in CodePlex/GitHub). ReSharper, for instance, promotes _ in its naming checks.

  • 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
  • 2
    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
  • 2
    @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
  • 1
    @jgauffin Aside from consumers using reflection against your code, isn't changing a field to a property transparent to callers? How does changing a protected field to a protected property of the same name break compatibility? – Quantic Jun 16 '16 at 15:32
  • 1
    it breaks binary compatibility: msdn.microsoft.com/en-us/library/ta31s3bc(v=vs.71).aspx – jgauffin Jun 16 '16 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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