Niyaz
Niyaz

Reputation: 54823

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?

Upvotes: 65

Views: 92677

Answers (8)

Carlos A Merighe
Carlos A Merighe

Reputation: 99

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 use _.

Upvotes: 2

Raktim Biswas
Raktim Biswas

Reputation: 4087

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;}
}

Upvotes: 15

Hamish Anderson
Hamish Anderson

Reputation: 21

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.

Upvotes: 1

Lalo Téllez
Lalo Téllez

Reputation: 46

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.

Upvotes: 1

ShuggyCoUk
ShuggyCoUk

Reputation: 36466

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503040

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...

Upvotes: 89

Steven Robbins
Steven Robbins

Reputation: 26599

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

Upvotes: 5

Ben Robbins
Ben Robbins

Reputation: 2630

In general, this means private member fields.

Upvotes: 37

Related Questions