What does variable names beginning with _ mean?

C#asp.netSyntaxVariablesNaming Conventions

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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 - C#

In general, this means private member fields.

Solution 3 - C#

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 - C#

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

Solution 5 - C#

There is another advantage with starting an instance property with '', it shows up first on Intellisense.
When creating a value/model/POCO class, I don't using '
'.

Solution 6 - C#

I know this is a little old, but in case anyone ends up here I'll add to the answers here that if you are talking about MVC on .NET, also there is a naming convention on partial views also with their names starting with underscore.

This are naming conventions that you and your team can decide to follow or not, as long as all of you are aware of the decision. I use them also with non public fields like private or protected just to recognize them.

Here's a little brief about it if you want to read further.

Solution 7 - C#

The use of an underscore for property private variables is very useful in avoiding situations where you may accidentally call the variable and not the property within the class itself. (Easily done with a simple lowercase intellisense input)

If you undertake some form of calculation or aggregation within the property get{} the last thing you want to do is miss this when your intention is to call the property in full.

Solution 8 - C#

I have seen it used as a variable name for parameters which are not being used as a way to 'hide' them away from the rest of the function.

I can't say this is a good or bad thing but it was effective at indicating that the parameter was not to be used in the function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiyazView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Ben RobbinsView Answer on Stackoverflow
Solution 3 - C#Raktim BiswasView Answer on Stackoverflow
Solution 4 - C#Steven RobbinsView Answer on Stackoverflow
Solution 5 - C#Carlos A MerigheView Answer on Stackoverflow
Solution 6 - C#Lalo TéllezView Answer on Stackoverflow
Solution 7 - C#Hamish AndersonView Answer on Stackoverflow
Solution 8 - C#ShuggyCoUkView Answer on Stackoverflow