why define this kind of variable?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I am quite new to c#, so this question may be quite stupid.

I saw some variables are defined as this:
ArrayList _Text= new ArrayList();

why _ is needed?
_
 
Jerry,

It's not needed, it's just a preference of the person coding it. The
underscore is used in some situations to indicate that the variable is a
field in a class, as opposed to a variable in the local scope (of a method
or a property).

Hope this helps.
 
The underbar is often used to specify a class member field. Most often the
initial letter is lower-case.

A field woud be called:

int _thInt;

The property that exposes the field would be called:

public int TheInt
{
get... set...
}

This is particularly useful if you ever intend to convert C# to VB using a
conversion tool. VB is non-case sensitive so the compiler gets confused by
the names theInt and TheInt so the underbar serves to differentiate between
them.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi,

It's not needed, it's naming convention used by the coder.

cheers,
 
Bob Powell said:
The underbar is often used to specify a class member field. Most often the
initial letter is lower-case.

A field woud be called:

int _thInt;

The property that exposes the field would be called:

public int TheInt
{
get... set...
}

This is particularly useful if you ever intend to convert C# to VB using a
conversion tool. VB is non-case sensitive so the compiler gets confused by
the names theInt and TheInt so the underbar serves to differentiate
between them.

The conversion tool should be able to do this for you. I hate seing either
_theInt or m_theInt as it ruines intellisense. I stick to the convention of
having camelStyle for members and PascalStyle for properties.

Now that is C#

My 2 cents...
- Michael S
 
Hi,

The conversion tool should be able to do this for you. I hate seing either
_theInt or m_theInt as it ruines intellisense.

How it's ruined ? IIRC it does work as expected , I inherit from a class
that use that convension and it did worked fine.
I stick to the convention of having camelStyle for members and PascalStyle
for properties.

Now that is C#


Humm, IIRC Fxcop have that as a warning, the problem is with languages that
does not differenciate between upper/lowercases as VB ,if somebody try to
derive your class in a VB project will find a problem with it.

OTOH I use the same convension, :)



cheers,
 
Humm, IIRC Fxcop have that as a warning, the problem is with languages that
does not differenciate between upper/lowercases as VB ,if somebody try to
derive your class in a VB project will find a problem with it.

Serve's 'em right for using VB :o)
OTOH I use the same convension, :)

Me too. I don't, however, expose public members differing only in case,
and I don't usually expose fields even as protected members. The only
case in which I design to access fields from a derived class is when the
derived class is declared inside the base class (usually a null object
implementation).
 
Steve Walker said:
Serve's 'em right for using VB :o)

LOL.

Converting VB to C# should be easy, converting C# to VB should be illegal!
=)

Hmm. I think we have laws for this already. Perhaps a environment law? Like
pollution or something.
Writing VB should be considered dumping toxic waste. Converting C# to VB is
an act of terrorism. =)

Visual Basic Sucks So Hard It Bends Light
- Michael S
 
Back
Top