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#?

  1. private int myInteger;
  2. private int MyInteger;
  3. private int mMyInteger;
  4. private int _myInteger;
  5. private int _MyInteger;
  6. 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.)

0
2019-05-07 00:40:03
Source Share
Answers: 14

Use camelCasing for exclusive variables like myInteger

Consider a coming before _ if the variable is a back-up for a building to lower complications -
Variable _myProperty for building MyProperty

0
2019-12-03 05:25:25
Source

I pass alternative 5 : private int _MyFoo

I do not see any kind of actual affordable benefit over _ myFoo though.

0
2019-12-03 02:39:45
Source

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.

0
2019-12-03 02:39:13
Source

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.

0
2019-05-12 09:48:31
Source

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

0
2019-05-12 09:28:57
Source

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.

0
2019-05-11 18:31:16
Source

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!

0
2019-05-11 18:22:51
Source

I make use of the adhering to naming system:

  • 1st (myInteger) for neighborhood scoped variables
  • 2nd (MyInteger) for public buildings
  • 4th (_ myInteger) for exclusive variables
0
2019-05-11 18:10:52
Source

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.

0
2019-05-11 18:01:58
Source

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.

0
2019-05-08 09:19:55
Source

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.

0
2019-05-08 09:13:34
Source

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.

0
2019-05-08 09:00:44
Source

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;
}
0
2019-05-08 08:53:15
Source

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.

0
2019-05-08 07:48:23
Source