Naming convention - underscore in C++ and C# variables

Stan picture Stan · Jun 29, 2010 · Viewed 172.7k times · Source

It's common to see a _var variable name in a class field. What does the underscore mean? Is there a reference for all these special naming conventions?

Answer

jdmichal picture jdmichal · Jun 29, 2010

The underscore is simply a convention; nothing more. As such, its use is always somewhat different to each person. Here's how I understand them for the two languages in question:

In C++, an underscore usually indicates a private member variable.

In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore. This usage has largely gone to the wayside with the advent of automatic properties though.

Before:

private string _name;
public string Name
{
    get { return this._name; }
    set { this._name = value; }
}

After:

public string Name { get; set; }