Matt Matt -4 years ago 166
C# Question

What is the C# static fields naming convention?

I have recently started using ReSharper which is a fantastic tool. Today I came across a naming rule for static fields, namely prefixing with an underscore ie.

private static string _myString;

  1. Is this really the standard way to name static variables? If so is it just personal preference and style, or does it have some sort of lower level impact? Eg Compilation JIT etc?

  2. Where does this style originate from? I have always associated it with C++, is that correct?

Answer Source

The Microsoft guidelines are silent about private fields, they are only concerned with publicly visible members.

Common conventions are camelCase, _camelCase and even sometimes the hangover from C++/MFC m_camelCase.

If you use camelCase without a prefix, your property backing fields will differ from the property name only in case, which is not a problem in C#, but won't work in a case-insensitive language like VB.NET.

So many people, including myself, like to use an underscore prefix so that the same standards can be used in all languages. In my experience, underscore is much more common than m_.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download