Just how do you call your exclusive variables in C#?
What is the most effective technique, the majority of generally approved naming conventions for exclusive variables in C#?
private int myInteger;
private int MyInteger;
private int mMyInteger;
private int _myInteger;
private int _MyInteger;
- Strange various other alternative
Which do you make use of and also why? (My firm is rather new to C# and also I would love to select one of the most "sector approved" method to attempt and also get involved in our coding criterion.)
Juval Lowy's IDesign C# Coding Standard is fairly preferred. This standard advises prefixing exclusive participant variables with "m_" (Option 6). That is what we carry out in our group.
private int m_myInteger;
Option 4 (_myInteger
) is an appropriate variant of this standard.
I really did not such as the MSDN referral (myInteger
), due to the fact that it makes it hard to inform an exclusive participant from a neighborhood variable. Their referral, certainly, addresses this trouble by certifying exclusive participants with this
, which appears repetitive to me.
with the emphasize.
Costs Wagner clarifies why in Effective C#. Yet I would certainly never ever call an integer my Integer ,. far better something like _ age or _ size. Consisting of the TypeName in the instance name is a dreadful technique. Names need to be self informative and also given that C# is Type - Safe kinds can be figured out att perpetuity.
I do alternative # 4 since that's what the SSCLI resembles, yet truthfully I uncommitted that much on naming of exclusive variable. Public is a various tale.
BTW you neglected m_MyInteger
private int integer
If you get perplexed in between participant and also neighborhood variables in a method extent after that you possibly require to refactor.
I think the most effective means to do it (in C#/. net anyhow) is a mix of 2 and also 6:
private int MyInteger { get; set; }
There is in theory no variable in all below, yet it looks and also imitates an exclusive instance variable. If we require to add some organisation reasoning to that value (it's an entirely inner value, so we can do anything we intend to it nevertheless) after that it's currently 'propertyized' for us. A warm steaming mug of win!
The MSDN class layout guidlines http://msdn.microsoft.com/en-us/library/ta31s3bc.aspx advises alternative 1 - myInteger.
I have actually constantly utilized this design. I have an individual disapproval for the _ personality.
I make use of alternative # 4 over :
private int _myInteger;
.
I such as to have some sign of extent in my variable names, and also the emphasize suffices for that objective. It's additionally fairly very easy to read.
First off, PascalCasing is generally booked for public buildings, consts, approaches, etc of the class. So I would certainly miss 2 and also 5.
Second, hungarian symbols is inhibited in the.NET globe, so (uh, I assume) 3 is appropriate out. Thinking that's what is happening with 3.
That entrusts camelCasing and also _ camelCasing. I commonly make use of _ camelCasing for class variables, and also simple old camelCasing for variables scoped to a method or narrower. Camel covering is the approved criterion made use of for method debates, protected/private variable names and also variables within a method or narrower extent.
I additionally such as to prepend with the emphasize to make sure that my exclusive variables are organized in my intellisense. Nonetheless, I just do this for variables scoped to a type. Variables proclaimed within a method or narrower extent I leave the emphasize off. Makes it very easy to maintain them different and also maintain much less previously owned variables with each other.
In C++ I often tend to make use of _ as I switch over editors a whole lot which does not permit me to see if it's exclusive.
For C# I often tend to to leave the _ away as Visual Studio permits me to see if it's exclusive.
I often tend to make use of the Camel Case means to do this.
I assume the alternative 4 is actually one of the most legible alternative. It aids you from needing to do this :
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
It additionally makes all exclusive participants extra recognizable. In the copying, where the hell is age
originating from? Without the this
qualifier it is tougher to inform.
private void Method()
{
var x = 2;
var y = age + x;
}
This is way less complicated to recognize :
private void Method()
{
var x = 2;
var y = _age + x;
}
I make use of 4 (private int _myInteger;
) due to the fact that :
private int myInteger;
This is just how I call my neighborhood variables.
private int MyInteger;
This is just how I call constants.
private int mMyInteger;
This is not C# design.
private int _MyInteger;
This looks weird.