473,587 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

underscores on variable names

Hi

I am looking at some C# code, and can see in some of the classes there are
instance variables whose names start with an underscore, for example:

private string _projectId;

Is there a reason for using underscores like this in C#?

Thanks,
Peter
Nov 17 '05 #1
10 3172
Peter,

Personal preference, the most I see used at the moment is camelCase or other
styles like that.

Cor
Nov 17 '05 #2
"Peter Kirk" <pk@alpha-solutions.dk> a écrit dans le message de news:
uM************* @TK2MSFTNGP12.p hx.gbl...
I am looking at some C# code, and can see in some of the classes there are
instance variables whose names start with an underscore, for example:

private string _projectId;

Is there a reason for using underscores like this in C#?


Some folks use it as a prefix to distinguish a field from a property without
an underscore; Delphi programmers use a convention of prefixing fields with
'F', I have also seen ''m' used in C# (to denote memory I guess ?)

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #3
Just google for "C# naming conventions" or "C# coding style guide", you'll
find lots of infos :-)

"Joanna Carter (TeamB)" <jo*****@nospam forme.com> schrieb im Newsbeitrag
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
"Peter Kirk" <pk@alpha-solutions.dk> a écrit dans le message de news:
uM************* @TK2MSFTNGP12.p hx.gbl...
I am looking at some C# code, and can see in some of the classes there
are
instance variables whose names start with an underscore, for example:

private string _projectId;

Is there a reason for using underscores like this in C#?


Some folks use it as a prefix to distinguish a field from a property
without
an underscore; Delphi programmers use a convention of prefixing fields
with
'F', I have also seen ''m' used in C# (to denote memory I guess ?)

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 17 '05 #4
Note that this practice isn't official because it isn't CLR compliant.

I use the following naming conventions in my code. Similar styles are used
by others;

Underbar denotes a field. Fields are ALWAYS protected or private. The name
is the same as the property name but has a preceeding underbar and a lower
case initial letter such as _myInteger

Property names use the name of the property backer variable witout the
underbar and with the initial capital letter such as MyInteger.

A property is structured so:

protected int _myInteger;

public int MyInteger
{
get{return _myInteger;}
set{_myinteger= value;}
}

All names are CamelCased

This prctice is particularly useful if you need to provide code in both C#
and VB as I do often because I can write in C# and convert to VB using a
conversion tool. If you don't use the underbar in this way VB, which is too
stupid to recognise the difference between lower-case and upper-case
characters, chokes on the names myInteger and MyInteger being the same.

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

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.

"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:uM******** *****@TK2MSFTNG P12.phx.gbl...
Hi

I am looking at some C# code, and can see in some of the classes there are
instance variables whose names start with an underscore, for example:

private string _projectId;

Is there a reason for using underscores like this in C#?

Thanks,
Peter

Nov 17 '05 #5
I use the underscore, not to distinguish a field from a property,
but to "underscore " that this is a private member.
Some use "m" or "m_" but they all use it to say that this is a private field
member.
And some of us does'nt use this notation at all, but rather use "this"
keyword or nothing at all.

This is partly to make it easier to read, and partly to distinguish local
variables
from private members, which would otherwise potentially have the same name.
class A
{
private int value;
/* Sets the private member to the value specified. */
void SetValue ( int value )
{
// Would not work since we are assigning the parameter variable.
value = value;
// OK. This is a way to distuingish a local variable
// and a private member variable that have the same name.
this.value = value;
// OK (assuming the private member "value" was renamed "_value").
// If we always use some special character as a prefix on private
members,
// we will never have this "issue".
_value = value;
}
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Joanna Carter (TeamB)" <jo*****@nospam forme.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
"Peter Kirk" <pk@alpha-solutions.dk> a écrit dans le message de news:
uM************* @TK2MSFTNGP12.p hx.gbl...
I am looking at some C# code, and can see in some of the classes there
are
instance variables whose names start with an underscore, for example:

private string _projectId;

Is there a reason for using underscores like this in C#?


Some folks use it as a prefix to distinguish a field from a property
without
an underscore; Delphi programmers use a convention of prefixing fields
with
'F', I have also seen ''m' used in C# (to denote memory I guess ?)

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 17 '05 #6
The m prefix is a holdover from the days of MFC when the prefix m denoted a
member variable to distinguich them from local variables.

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

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.

"Joanna Carter (TeamB)" <jo*****@nospam forme.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
"Peter Kirk" <pk@alpha-solutions.dk> a écrit dans le message de news:
uM************* @TK2MSFTNGP12.p hx.gbl...
I am looking at some C# code, and can see in some of the classes there
are
instance variables whose names start with an underscore, for example:

private string _projectId;

Is there a reason for using underscores like this in C#?


Some folks use it as a prefix to distinguish a field from a property
without
an underscore; Delphi programmers use a convention of prefixing fields
with
'F', I have also seen ''m' used in C# (to denote memory I guess ?)

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 17 '05 #7
> Some folks use it as a prefix to distinguish a field from a property
without
an underscore; Delphi programmers use a convention of prefixing fields
with
'F', I have also seen ''m' used in C# (to denote memory I guess ?)

"module" actually, if memory serves. It would be an example of scope
specification in a hungarian variable name: m_lpzstrName = module field that
is a long(I *think*) pointer to a null terminated string. I don't recall for
sure what the l meant in lp.

Anyway, the common prefixes I can recall are
p for parameter,
m for module,
g for global,

probably others I don't know.

Nov 17 '05 #8

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:e%******** *******@TK2MSFT NGP15.phx.gbl.. .
Some folks use it as a prefix to distinguish a field from a property
without
an underscore; Delphi programmers use a convention of prefixing fields
with
'F', I have also seen ''m' used in C# (to denote memory I guess ?)

"module" actually, if memory serves. It would be an example of scope


Err, member. Thanks Bob.
Nov 17 '05 #9
Bob,

I was making my message to you in this thread before I saw this one.

:-)

Sorry

Cor
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
2905
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would using myclass.len() instead of myclass.__len__() really cause Python considerable harm? As much as I adore Python, I have to admit, I find this to...
84
5869
by: Andy Glew | last post by:
I am in search of any rigourous, scientific, academic or industrial studies comparing naming conventions in C++ or similar languages such as Ada: Specifically, are names formed with underscores more or less readable than names formed with MixedCase StudlyCaps camelCase?
2
3192
by: Kari Laitinen | last post by:
During the past 15 years I have been writing computer programs with so-called natural names, which means that the names (identifiers, symbols) in progarms are constructed of several natural words. I have even written a C++ book in which all programs are written with natural names. More information and free pages of the book can be found at...
22
2231
by: BRIAN VICKERY | last post by:
I've heard that some compilers reserve the '_' starting a symbol for system variables/symbols. I can not find this documented anywhere (so far). I've used an '_' at the end of member variables (ie. Uint16 bar_; ) to distinquish private fields, but this does not stand out as much as if the '_' were at the begginning of the name as in the...
46
2692
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names. Looking at the examples below, which ones seem better? Straight names echoclient lastcharoffset helloworld Internal underscores
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.