views:

372

answers:

5

What is the universally accepted naming convention for c#? (functions, classes, parameters, local variables, namespaces, etc)

+3  A: 

The .NET standard from Microsoft is to use Pascal Case for namespaces, public and protected members (basically anything visible to other classes). For private members and local variables, there's a much wider berth to just do whatever you and your team are most comfortable with.

Rex M
+16  A: 

Microsoft has an excellent set of guidelines on class library design, including a section on naming. In short:

  • Classes/Structs: PascalCase (WebRequest)
  • Interfaces: PascalCase with I prefix (IDisposable)
  • Methods: PascalCase (ToUpper)
  • Namespaces: PascalCase (System.Collections; unusual to have two words in one part though)
  • Non-constant variables including parameters: camelCased
  • Constants: PascalCase (Int32.MaxValue)
  • Enums: PascalCase, singular for non-flags and plural for flags (HttpStatusCode, BindingFlags)
  • Attributes: PascalCase with "Attribute" suffix (ThreadStaticAttribute)

Private names are up to you, but I tend to follow the same conventions as for everything else. Hungarian notation (in the style of Win32) is discouraged, although many places use "m_" or "_" as a prefix for instance variables.

Jon Skeet
A: 

Juval Lowy took a stab at this is in Programming .NET Components, see this SO link too.

kenny
+1  A: 

Don't underestimate the value of following the naming conventions of the platform you are working on as closely as possible.

Look at the reference material for the .NET Framework for examples of how to "fit in" (http://msdn.microsoft.com/en-us/library/ms229335.aspx).

Jon Skeet has given you a link to a good writeup by Microsoft: http://msdn.microsoft.com/en-us/library/ms229042.aspx

You can also use the standalone Microsoft FxCop (or Code Analysis if you have the Team Edition) http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&DisplayLang=en to check that the naming conventions have been followed. It has built-in rules for the Microsoft conventions, which is another reason you should be using them!

binarycoder
+1 for the FxCop suggestion
Nestor
A: 

I'd have a look at the slim book called "Elements of C# Style" by Baldwin, Gray, & Misfeldt. The blue book covers naming conventions, and many other aspects of creating consistent, clean, readable code.

-Paul

CenterSpace Software
.NET math libraries

Paul