Fields in C#

by KodefuGuru 5. August 2009 12:25

Fields… not the kind with green grass and butterflies. Rather, fields that contain the deep dark secrets of a class. You know: member variables.

There’s plenty of divergence in naming these pieces of class data. Some developers prefix the field name with an underscore. Some prefer m (meaning member) and an underscore.  I personally prefer straight camelCasing. Here are some variations I’ve seen.

  1. private string name;
  2. private string _name;
  3. private string m_name;
  4. private string _Name;
  5. private string m_Name;
  6. private string mName;

There’s a lot of argument to be had concerning, and I was involved in a debate yesterday concerning this. A fellow developer in Greenville claimed that camelCasing was a bad practice because it confuses VB developers. He was advocating for PascalCased fields (and even regular variables / parameters… ::shudder::).

Here’s the deal. PascalCasing private member fields and variables is confusing to me as a C# developer. When I see PascalCasing, I assume that it’s part of the interface of the class. When I see camelCasing, I assume it’s private. Besides, I will

I find prefixing with an “m” rather bizarre. I believe this may be a holdover from some other language. I’ve heard the argument that it helps you differentiate between your members and local variables within a method. I would respond that underscore works just as well for that purpose, and your methods are too long if you couldn’t differentiate between the two.

This leaves underscore. I just think underscores are ugly. They are practical when a parameter name is the same as a field name, but I scope my variables in that situation which is the recommended practice anyway (e.g. this.name = name).

Number 1 (camelCasing with no underscore) is the rule I’ve always followed because it felt most natural. But I can also say definitively that it is the accepted best C# styling practice. StyleCop rules SA1306 and SA1309 dictate that field names must begin with a lower case letter and must not begin with an underscore. For those that think “aha, m_ it is,” SA1310 states that field names must not contain an underscore.

The only time you should violate this rule when naming a field is when the field is not private. The only time your field should not be private is when it marked readonly. Be careful when doing this, as this will cause a change to your interface if you need to promote the field to a property. The only time I’ve seen a real use for this is with static readonly fields.

I’ve seen some people expose fields as protected. When this happens, it is important to remember that protected means the fields are public to derived classes. In other words, you have broken encapsulation. Encapsulate your fields as properties when you need to expose them anywhere outside of the class (except for static readonly fields, shorthand for “I need a const but need an instance in this context”).

That concludes my rant on fields. I’m sure some will disagree… feel free to voice your opinion in the comments. Keep in mind that unless your authority is greater than StyleCop (aka the styling authority of Microsoft), I will be hard to persuade. At the same time, I understand that different organizations adopt different styles for different reasons, and that’s find for your organization as long as you don’t start breaking framework guidelines as those affect consumers of your product.

Tags: , ,

Kodefu

Comments

8/5/2009 12:27:58 PM #

trackback

Trackback from DotNetKicks.com

Fields in C#

DotNetKicks.com

8/5/2009 12:30:41 PM #

trackback

Trackback from DotNetShoutout

Fields in C#

DotNetShoutout

8/5/2009 1:47:16 PM #

Jon Arild Tørresdal

Great article where you get many of the pros and cons for the different options, though I will use my right to disagree Smile

There are as many opinions around this topic as developers in the world. Though there are some things that makes more sense than others. For me there are only two options. Either camel case or underscore + camel case. I tend to prefer underscore because I don't like using 'this' on local variables. By using underscore it's perfectly clear that it's a member variable, hence if it's camel case it's a local variable. That's how I like it Smile

Jon Arild Tørresdal Norway

8/5/2009 6:36:31 PM #

Sebastian

Great article.

As jont I prefer underscores for the same reasons, I don't like "this" and camelCase = local variable Smile

Anyways, you should only use a single style and stick to it.

Sebastian Germany

8/6/2009 12:12:30 AM #

Justin James

For a while I was using underscores to not compete with local variables as well. Otherwise, I'd either need to use "this" (which could be a dangerous ommission that compiles just fine), or be doing weird things with my local variable naming (like "mySameNameAsTheMemberField"). Indeed, I think the "m_" probably was a euphamism for "my".

Lately, though, I've been using a lot more autoproperties, and in exchange, when I *do* make a backing field, I give it a name that makes it really clear that it is a backing field. Since it should only be used in a few lines of code in the accessor, I don't mind having a backing field with a slightly different name from the property.

J.Ja

Justin James United States

8/6/2009 9:22:03 AM #

manuel0081

Better with underscore. Think if there is a parameter with the same name in a constructor...

manuel0081 Italy

8/16/2009 6:58:45 AM #

trackback

Trackback from #.think.in

#.think.in infoDose #40 (5th August - 16th August)

#.think.in

8/18/2009 5:30:17 PM #

Adam Vandenberg

"Better with underscore. Think if there is a parameter with the same name in a constructor..."

public Something(int a, string b){
  this.a = a;
  this.b = b;
}

... I don't see the problem.

Adam Vandenberg United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er


World Map

RecentComments

Comment RSS

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010