47

There are a few questions on this, but they all seemed to be targeting a specific part of the language;

I'm just starting out in C# with a friend on a venture to create games for XBOX Live Arcade. I've developed a number of games using ActionScript 2 and 3 but want to start exploring more powerful languages and devices.

I want to ensure that I don't peeve people that I start working with (if I get to that) or even just people on here when I run into trouble and ask a question with seriously disturbing / "incorrect" naming of methods, etc.

I've found it confusing in the example code that I've seen because there seems to be from my current point of view some flaws. I doubt that a flawed naming convention would be used, so I realize that I'm just having trouble understanding.

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

  • public Type SomeMethod()
  • private Type SomeMethod() - no underscore or anything?
  • public static Type SomeMethod()
  • private static Type _SomeMethod() - this one just seems odd..
  • public Type someProperty - switching it up to camel casing for properties?
  • public static Type SomeProperty - and then going back to pascal casing for static..

In ActionScript 3, I have developed and strictly stick to these conventions:

  • private var _someVar
  • public var someVar
  • private function _someMethod()
  • public function someMethod()
  • public static var SomeStaticVar
  • public static function SomeStaticMethod()
  • public const SOME_CONSTANT

Is there a complete list of naming conventions with reasoning behind each so that I can get my head around them? The reversal of syntax (i.e. public Type method() instead of AS3's public function method():Type) is throwing me out enough at the moment that I know I need to keep an eye on how I'm naming things, otherwise I'll forget and develop bad habits, which I'd rather nail and avoid now.

2

4 Answers 4

58

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 _
8
  • 1
    @ Henk. Wat about protected and static fields ? May 31, 2013 at 10:15
  • 4
    _ is forbidden in Microsoft's naming convention.
    – Yoda
    Jan 6, 2014 at 20:26
  • 1
    @Yoda - what version/year? MS was adamant about that when Fx 1 came out but a few yars later backing fields were commonly prefixed.
    – H H
    Jan 6, 2014 at 20:43
  • 1
    @Mike, and at the neighbour page for Type Members: "Internal and private fields are not covered by guidelines". My answer refers to what I see being used in practice.
    – H H
    Sep 5, 2014 at 7:26
  • 2
    @Trisped Yet MS still uses them in their own code samples. Oct 15, 2015 at 20:25
17

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.

4
  • Have to pay for scribd
    – rollsch
    Oct 21, 2016 at 12:38
  • 1
    @rolls You can view the document on scribd or codeplex for free. If you want to download it then use the codeplex link.
    – Nasreddine
    Oct 21, 2016 at 13:05
  • Thank you I just found that out.
    – rollsch
    Oct 23, 2016 at 1:52
  • @Nasreddine Codeplex was killed, link is no longeruseful Mar 13, 2020 at 12:03
7

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.

2
  • 1
    I guess the all-caps convention is a relict of old coding styles originating in C. This is definitely something you won’t find among standard .NET constants or enumerations.
    – Palec
    Oct 21, 2015 at 18:33
  • I use ALLCAPS for enums and constants in C# still as most of the time I'm using them with interop C++ DLLs
    – rollsch
    Oct 21, 2016 at 12:40
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.