iDillon iDillon - 2 years ago 74 Question

C# and VB Class Private Variable Naming Practices and the "_" Character

Let's be clear: I'm not looking for an opinion, but an answer, an actual documented source or reference if possible. (Preferably something from MSDN?)

In .NET when creating a class, the basic structure I follow goes along the lines of:

  • Creating private variable fields.

  • Defining the class properties using get/set.

  • Creating the constructors.

  • Adding extra class methods for database functionality.

  • Misc functionality.

How I used to name my private variables:

private string _myString;

How I currently name my private variables:

private string iMyString;

I have been told that using the "_" character for private variable declarations has negative effects. which I then changed my practice to use the "i" character. I find that using the same character for these declarations helps with the speed of creating class properties, and overall development time.

My question is: What is the industry standard to the class private variable naming structure? AND, Why is using the "_" character considered a bad practice?

I've done quite a bit of google searches and even on the MSDN website the pages I viewed had their examples change regularly and thus inconsistent.

I'm not sure if this is considered a problem per say, but I tend to practice quality code.

Answer Source

From the .NET foundation coding style guidelines:

We use _camelCase for internal and private fields and use readonly where possible. Prefix instance fields with _, static fields with s_ and thread static fields with t_.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download