C# Express: Pick a name, any name.


I recently got into bother because of my rather lax use of variable names in some example code. As someone who has the job of writing code samples that programmers like you are going to read when you glimpse at the C# documentation, it’s important that I at least set a good example. So I read up on the .NET naming conventions, and thought you might like to see them too – especially if you are just starting out with C# and .NET programming, and want to make sure your code looks as polished as the professional programmer you are claiming to be in your resume.

 

Will your choice of naming convention stop your code from compiling? No. Will your choice of names make your code run faster? No.

 

What a consistent naming convention will do is make it easier for you when to go back to look at your code, trying to work out what is going on. And if everyone sticks to the same naming style, it will make it easier when you look at someone else’s code, and easier when they read yours.

 

If you came to C# from C++, you might be used to “Hungarian” naming convention. This convention included a little bit of the variable type in the variable name itself, as a clue. So a string variable might be called “strMyName” for example. Now, as C# is a strongly typed language, this isn’t really necessary – if Intellisense doesn’t spot it, the compiler sure as heck will.

 

All this information comes from a section in the MSDN docs called Naming Guidelines, and here are the highlights. Firstly, don’t use underscores. And then follow these specific examples:

 

[Edit: Using underscores is actually acceptable, except in classes and interfaces. At least, underscores aren't specifically banned elsewhere.] 

 

Namespace Names

 

The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design: e.g. Microsoft.Media.Design.

 

Class Names

 

Use a noun or noun phrase, and Pascal case. Don't use special letters (F or C) to precede the name (this isn't MFC ;-). Examples: Button, FileStream.

 

Interface Names

 

Use a noun or noun phrase, and Pascal case. Start the name with a capital I. For example, IServiceProvider, IFormatable.

 

Parameter Names

 

Use descriptive names, and Camel casing. For example, typeName, personName, personAge.

 

Method Names

 

Use verbs or verb phrases, and Pascal case. For example, RemoveAll(), GetFileData().

 

Property Names

 

Use a noun or noun phrase and Pascal case. For example, BackColor.

 

Note:

 

If a name is in Pascal case, it starts with a capital letter, and so do any other words that make up the name. For example, BackColor.

 

If a name is in Camel case, then it starts with a lower case letter, and the first letter of every subsequent word is in upper case.  For example, trouserPress.

Comments (8)

  1. Tom Frey says:

    Why are underscores a nono for private members?

    I love them as I can quickly see what’s a private member or maybe a local variable or parameter.

    How should they be named instead?

  2. John says:

    You know, I think I was a little harsh with the ban on underscores. Re-reading the rules indicates that it’s ok to use them in your context. Man, is it any wonder I get into trouble.

  3. I still think a bit of Hungarian helps. It is much easier to read code without looking up variable or parameter declaration so

    strA + strB

    is string concatenation as opposed to sum of integers. Hungarian is not about helping compiler or avoiding compilation errors. It is about code readability in the first place.

    And intellisense in C# does not work if file is outside of a project.

  4. C++ is strongly-types as well, of course!

  5. This is good stuff. Some of your best work.

  6. John says:

    It’s my best work, because I didn’t write it – I just lifted it from MSDN 😉

    Here’s some comments on Strong Typing:

    http://c2.com/cgi/wiki?StronglyTyped

    I bow to those with greater knowledge of the subject than me. BTW, in case you think I’m biased, C is my favourite programming language. Well, C and Z80 assembly language (I learn programmong on a ZX80, ZX81, Jupiter Ace, ZX Spectrum, Amstrad CPC and Amiga – heck if you know your Amiga, and you’re from the UK, I was editor of a magazine called "Amiga Computing" many years ago).

    XiD or xID or XDi.. etc etc.. well, what context is xiD going to be used in?

  7. Scot Boyd says:

    You forgot to mention variable names. I would assume they’re supposed to be Camel case as parameters are.

Skip to main content