What does variable names beginning with _ mean?

When writing my first asp.net MVC application using C#, I see that there are some variables whose name start with an underscore character(_).

What does this mean? Is there any specific meaning for this?


Solution 1:

There's no language-defined meaning - it's just a convention some people use to distinguish instance variables from local variables. Other variations include m_foo (and s_foo or g_foo or static variables) or mFoo; alternatively some people like to prefix the local variables (and parameters) instead of the instance variables.

Personally I don't use prefixes like this, but it's a style choice. So long as everyone working on the same project is consistent, it's usually not much of an issue. I've seen some horribly inconsistent code though...

Solution 2:

In general, this means private member fields.

Solution 3:

The underscore before a variable name _val is nothing more than a convention. In C#, it is used when defining the private member variable for a public property.

I'll add to what @Steven Robbins said:

private string _val;
public string Values
{
    get { return _val;}
    set {_val = value;}
}

Solution 4:

A lot of people use them for property private variables (the variables that actually store the values for public properties).