Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

What does the _ indicate when used in front of a variable name?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
katzky - 30 Aug 2006 18:36 GMT
At one point, I knew the answer to this question.  Unfortunatley it has
slipped my mind [but I still remember how difficult it was to search for the
answer a couple of years ago]

i.e. Difference between x and _x ?

Thanks.
Mark Rae - 30 Aug 2006 18:48 GMT
> At one point, I knew the answer to this question.  Unfortunatley it has
> slipped my mind [but I still remember how difficult it was to search for
> the
> answer a couple of years ago]
>
> i.e. Difference between x and _x ?

It means that the person who wrote the code is very old... ;-)
ThunderMusic - 30 Aug 2006 19:00 GMT
hi,
it usually means that x is a member variable... many naming convensions are
available all over the net... naming member variables with an underscore
prefix is part of one of them...  another one sugget to use "m_" instead of
just "_" so it would give m_x

I hope it helps...

ThunderMusic

> At one point, I knew the answer to this question.  Unfortunatley it has
> slipped my mind [but I still remember how difficult it was to search for
[quoted text clipped - 4 lines]
>
> Thanks.
Bruce Wood - 30 Aug 2006 19:08 GMT
> At one point, I knew the answer to this question.  Unfortunatley it has
> slipped my mind [but I still remember how difficult it was to search for the
> answer a couple of years ago]
>
> i.e. Difference between x and _x ?

To the compiler, no difference at all. It's just another name.

However, we, for example, use the _x convention for member variables.
The biggest advantage is with Intellisense. We used to do this:

private string description = "";

public string Description
{
   get { return this.description; }
   set { this.description = value; }
}

but since Intellisense is case-sensitive, we often had errors in the
code that used the field instead of the property, or vice versa. With
the underscore, there's no ambiguity in Intellisense:

private string _description = "";

public string Description
{
   get { return this._description; }
   set { this._description = value; }
}

The other advantage is that in the debugger, the "_" is sorted before
"a", so all of the fields show up at the beginning of an object's
state, so there's no need to search for them.
Greg Young - 30 Aug 2006 19:40 GMT
Another common use of the _ is to prefix parameters to a method .. often
this is done with m_ prefixing members.

ex:

public class foo {
   print int m_Bar;
   public void DoSomething(int _Bar) {
        m_Bar = _Bar;
   }
}

> At one point, I knew the answer to this question.  Unfortunatley it has
> slipped my mind [but I still remember how difficult it was to search for
[quoted text clipped - 4 lines]
>
> Thanks.
Peter Bromberg [C# MVP] - 30 Aug 2006 22:13 GMT
Another advantage of using _varName besides what Bruce indicated, is that if
you select
private int _varName in your code and choose Refactor in VS 2005, and create
a property, it will recognize the underscore and create all your property
code perfectly with a

public int VarName

propertyName, saving a lot of tedious repetitive, and mind-numbing code -
writing.

Peter
Signature

Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

> At one point, I knew the answer to this question.  Unfortunatley it has
> slipped my mind [but I still remember how difficult it was to search for the
[quoted text clipped - 3 lines]
>
> Thanks.
Joanna Carter [TeamB] - 30 Aug 2006 22:58 GMT
| Another advantage of using _varName besides what Bruce indicated, is that if
| you select
[quoted text clipped - 6 lines]
| propertyName, saving a lot of tedious repetitive, and mind-numbing code -
| writing.

This works just as well if you declare a field :

private int varName;

... and then invoke the Encapsulate refactoring.

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

Peter Bromberg [C# MVP] - 31 Aug 2006 20:28 GMT
Of course! My point was, it recognizes the underscore as a valid coding
construct and removes it from the public field declaration.
Peter

Signature

Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

> | Another advantage of using _varName besides what Bruce indicated, is that
> if
[quoted text clipped - 16 lines]
>
> Joanna

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.