C# – Capitalization convention for C# protected fields

ccoding-standardsnet

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

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

Best Answer

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.

Related Topic