What are the naming conventions in C#? [closed]

Solution 1:

The two main Capitalizations are called camelCase and PascalCase.

The basic rules (with lots of variations) are

  • Types use PascalCase
  • properties and methods always use PascalCase
  • public members (fields, consts) use PascalCase
  • local variables use camelCase
  • parameters use camelCase

And although the documentation states that "Internal and private fields are not covered by guidelines" there are some clear conventions:

  • private fields use camelCase
  • private fields that back a property prefix a _

Solution 2:

There is the All-In-One Code Framework Coding Standards from Microsoft which contains a complete set of rules and guidelines. (also used to be available here)

This document describes the coding style guideline for native C++ and .NET (C# and VB.NET) programming used by the Microsoft All-In-One Code Framework project team.

Solution 3:

There are a whole lot of naming conventions advocated by Microsoft for .Net programming. You can read about these here.

As a rule of thumb, use PascalCase for public property, method and type name.

For parameters and local variables, use camelCase.

For private fields, choose one: some use camelCase, other prefix _camelCase with an _.

A commonly seen convention is also to name constants with ALLCAPS.

Solution 4:

C# prefers PascalCasing for classes, properties, and methods.

As far as I can tell so far, there are these conventions:

  • public Type SomeMethod() <-- yes
  • private Type SomeMethod() <-- correct, no underscore
  • public static Type SomeMethod() <-- correct
  • private static Type _SomeMethod() <-- this seems odd to me too. underscore should not be there
  • public Type someProperty <-- no, a public property should be PascalCased (SomeProperty)
  • public static Type SomeProperty - and then going back to pascal casing for static..

If you are using Visual Studio, or XNA Game Studio (which I think is a fork of Visual Studio), I highly recommend springing for a ReSharper license (from jetbrains software). They will tell you, in your code editor, how to conform to common C# conventions.

Addition:

You should use camelCasing for private fields and method arguments. For private fields, I usually prepend them _withAnUnderscore.