Naming Conventions for C#

I'm a firm supporter of coding conventions (at least of my coding conventions).

Software factories [^] [^] [^] and other code generation tools have been taking care of writing the tedious (and, sometimes, ugly) code but, at some point, some code must be written and read by human developers. That's why (in my opinion) the way the code is written is as much important as the language it is written in.

There are several resources about coding conventions for the .NET framework:

I haven't read Brad and Krzysztof's book (yet), but I've read MSDN Library's guidelines and Juval's document and watched Krzysztof's presentation.

I have to disagree with Microsoft's position about naming conventions of private members. In an enterprise as big as Microsoft projects start and end, people move between teams and every time someone changes team he/she is faced with the possibility of having to learn and adapt to a new set of naming conventions for private members.

IDesign goes one step further on defining naming conventions for private members. I just don't like the convention they used. What's the point of prefixing private member fields (not variables as in their document) with m_? What does it mean? Member? If it's not a local method variable or a method parameter it's a member.

Some just prefix with _. Once again, why? What does that mean?

The only reason I can find for this prefixing practice is lost in the old days of colorless IDEs, printers and books. With colorful IDEs like Visual Studio 2005 you just need to qualify instance member fields with the this (Me for Visual Basic) keyword and static (Shared in Visual Basic) member fields with (lacking of a better solution) the class name.

Lets pretend for a moment that I'm one of the prefixes. How would I do it?

  • i_camelCase for private instance members.
  • s_camelCase for private static members.
  • c_camelCase for private constants.

Why stop there? Let's also prefix method variables:

  • v_camelCase for method variables.

Now I'm on a roll. Let's prefix method parameters:

  • i_camelCase for input parameters.
  • o_camelCase for output parameters.
  • r_camelCase for by reference parameters.

The next step would be to go Hungarian and add type information to the names, right? No. Let's stop here and get in the right track.

My Naming Conventions for C#

I Find that keeping naming conventions as simple as possible is the best way use and validate them.

  1. Pascal casing for any class/struct member (constants, fields, properties, methods, events) except for private fields.
  2. Camel casing for private fields and method/property parameters and variables.
  3. Prefix interface names with I (IAccountService).
  4. Prefix type parameter names with T (TKey, TValue).
  5. Suffix type with meaningful names for its purpose (AccountService, InvalidAccountException, MustVerifyAccountAttribute, FromAccountTextBox).
  6. Method names must reflect what they are supposed to do. Methods always do something, so they must start with a verb. (GetAccountInformation, UploadFile)
  7. Names must be descriptive. Abbreviations should not be used. Hungarian notation should not be used. Single name variables should not be used.
  8. Language keywords should be used instead of type names (string instead of System.String, int instead of System.Int32, etc.).
  9. Business relatad names should reflect business entities or actions, and in the business' language (GetContaFromDataBase, ContaInválidaException, ValidarContaAttribute).
  10. Underscores should not be used. Camel casing should be used to separate words.
  11. Acronyms should be treated as a single word. Just to be compliant with Microsoft, for acronyms with less than three letters, all letters should be upper case.
Published Sunday, May 20, 2007 11:28 PM by Paulo Morgado

Comments

Monday, May 21, 2007 2:39 PM by luisabreu

# re: Naming Conventions for C#

why use _? good question. well, if you think about vb (which wasn't case sensitive the last time i've looked), then it may make sense for instance, say you've got an IdAccount field. using idAccount for the field and IdAccount for the property won't work. so, that's why i use the _ for private fields (and yes, I've noticed that you've set the guidelines for C#, but to me, it still makes sense to follow this convention for c#). . I agree with most of the other things, though i still prefer another convention for control members.
Monday, May 21, 2007 3:16 PM by Paulo Morgado

# re: Naming Conventions for C#

If I were a Visual Basic programmer, I would sufix private fields with Field and private events with Event.

But this case "sensitiveness" of C# is one of the things I like about it (and, also, one of the things I don't like about Visual Basic).

Tuesday, May 22, 2007 4:44 PM by luisabreu

# re: Naming Conventions for C#

Well, i'm not sure if case sensitivity is a good thing. Jeff "horror" Atwood doesn't really like it and i think i agree with what he says here:

www.codinghorror.com/.../000860.html

Tuesday, May 22, 2007 5:53 PM by Paulo Morgado

# re: Naming Conventions for C#

Well, I don't.

But I can see you do.

Just looking at the way we write our names everyone can see the importance each one of us gives to casing.

From Jeff's referenced article (www.somethinkodd.com/.../the-case-for-case-preserving-case-insensitivity) I only have to say that my name is Paulo Morgado and, unless it's a misstype, I wouldn't like to see it written as pAuLo MoRgAdO or some other variation.

Casing is important and you agree it is. That's why you use pascal and camel casing in your code instead of underscores separating words, right?

And why does the Visual Basic IDE keep correcting the casing if it's case insensitive? Why not uper case or lower case everything?

Wednesday, May 23, 2007 6:29 PM by Paulo Morgado

# My C# Naming Conventions For Partial Class Files

Following up on a previous post , this time I'll give you my naming conventions for partial class
Tuesday, May 29, 2007 6:57 PM by Miguel

# re: Naming Conventions for C#

Paulo:

Without the prefix how can you make a difference between a property and private member:

public class TestMeClass
{
  private int test;
  public int Test
  {
    get { return test; }
    set { test = value; }
  }
}

The main issue is not for C# but for case insensitive languages such as VB.NET or Delphi.NET ( In this case these languages can interpret the variable either way; also if you are making a class library that library can be used by any .NET client).

I agree with you idea of prefix, it does make the code stands out.

Tuesday, May 29, 2007 7:52 PM by Paulo Morgado

# re: Naming Conventions for C#

Miguel:

I can differentiate them by the casing.

These are "my" naming conventions for C#.

I do have a proposal for case insensitive languages: suffix fields with Field.

Since this convention is for private members only, there's no problem with consuming this classes using case insensitive languages.

Monday, July 23, 2007 5:14 PM by Kenneth Siewers

# re: Naming Conventions for C#

Well, I actually prefer using the underscore for prefixing my private fields. This is simply because they group together in Intellisense. Whenever I am looking for a private field, I just type in the underscore, and all my private fields show up (including the nasty "_AppDomain" interface Microsoft left behind).

Saturday, September 29, 2007 2:55 PM by Fluxtah

# re: Naming Conventions for C#

private int foo;
public int Foo
{
  get { return foo; }
  set { Foo = value; }
}

Check the code carefully, I've seen this happen and a dev take half a day for me to come over and say 'I see your problem, thats where your stack overflow problem is'

And he was astonished at my good eyes.

Granted, this is a head slapping mistake, maybe not enough coffee, but from my point of view having a more user friendly convention can avoid this issue, thats why I like using underscores.

private int _Foo; is much less prone to this;

Saturday, September 29, 2007 3:47 PM by Paulo Morgado

# re: Naming Conventions for C#

It's not without problems and It has happened to me a few times. Fortunately, this has been the only situation where I've got a StackOveflowException and I always know what and where to look.

But, on the other hand, that revels that that developer was unable to debug and he can do a lot worse and not so obvious mistakes. And no amount of prefixes can help you there.

Wednesday, November 21, 2007 10:57 PM by DotNetKicks.com

# Naming Conventions for C#

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, November 22, 2007 12:29 AM by Betty

# re: Naming Conventions for C#

So the following parameter for the function FindPerson hides the class member variable? that just seems silly. I like underscore prefixes.

public class Person {

  private int personId;

  public int PersonId {
    get { return personId; }
    set { personId = value; }
  }

  public void FindPerson(int personId) {
  }

}

Thursday, November 22, 2007 7:38 AM by Paulo Morgado

# re: Naming Conventions for C#

And what does the underscore prefix mean?

Thursday, November 22, 2007 12:41 PM by Betty

# re: Naming Conventions for C#

does it need to mean something? I would have thought the whole stopping name clashes was enough to warrant the use of another naming convention in that case. The underscore just happens to be what I use.

Thursday, November 22, 2007 3:50 PM by Paulo Morgado

# re: Naming Conventions for C#

I like when things mean something to me. And, since the underscore means nothing to me, it kind of gets in my nerves.

It's just the way I am.

Friday, December 14, 2007 7:47 PM by Craig

# re: Naming Conventions for C#

I use the underscore as well, to me it means private withing the scope of the class, so:

private int _Var;

public int Var
{
  get { return _Var;}
 set { _Var = value;}
}

and

private void _PrivateMethod()
{
}

and

public void PublicMethod()
{
}

Saturday, December 15, 2007 5:53 AM by Paulo Morgado

# re: Naming Conventions for C#

Wich is as good a convention as:

private int var;

public int Var { ... }

Tuesday, July 22, 2008 4:19 PM by C#/VB Conventions at Matt’s Blog

# C#/VB Conventions at Matt’s Blog

Pingback from  C#/VB Conventions at Matt’s Blog

Leave a Comment

(required) 
(required) 
(optional)
(required)