.NET Naming Conventions


I often get asked about the Naming Conventions I adhere to when writing code (C#, naturally).


It made sense to share these in a blog post so I can refer to it in future.


Some of these guidelines (well, one, the underscore on private fields) are negotiable as a matter of style. However, the public stuff is non-negotiable. For this is how .NET APIs should be and failure to adhere to this reflects badly on your code. No, no, no.


I thought a good way to present this would be an example class demonstrating the rules and some comments to help, so here goes:

using System;

// Namespaces are PascalCased

namespace TheJoyOfCode.NamingConventions
{
    // Class names are PascalCased

    public class ExampleClass
    {
        // All public fields, including constants are PascalCased

        public static const string PiAsAString = “3.14”;

        // All private fields are camelCased with an underscore [1]

        private readonly string _privateMember;

        // All protected members are PascalCased

        protected int ProtectedField = 12;

        // All internal members are PascalCased

        internal int InternalField = 13;

        // All private methods are PascalCased

        // *** NOTE – All parameters are camelCased
        private double Multiply(double valueA, double valueB)
        {
            // local variables (scoped within a method) are camelCased (no underscore)

            double result = valueA * valueB;
            return result;
        }

        // All private Properties are PascalCased

        // *** NOTE – Acronyms of 2 characters are UPPERCASED (e.g. UI, IO)
        private string UIElementName { get; }

        // All (public and private) properties are PascalCased

        // *** NOTE – Acronyms longer than 2 characters are PascalCased (e.g. Html, Xml)
        public int HtmlLength { get; set; }

        // All public methods are PascalCased

        // *** NOTE – All parameters are camelCased
        // *** NOTE – Abbreviations are not treated as Acronyms (so _Id_entification is Id, not ID).
        private void AlignObjectById(string id, Alignment alignment)
        {
            throw new NotImplementedException();
        }

        // Nested classes are PascalCased, even Private ones

        private class NestedClass : IDisposable
        {
            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }
    }

    // Enums are PascalCased and not plural (unless marked [Flags] in which case the name should be plural)

    public enum Alignment
    {
        // Enum members are PascalCased

        Top,
        Bottom,
        Left,
        Right,
    }
}

// [1] – Note the underscore isn’t as recommended by StyleCop but since it applies only to private members, can be considered a matter of style and one that I personally use.

… and as for #region blocks I do not use regions and I don’t negotiate with terrorists either.


Originally posted by Josh Twist on 8 September 2009 here.

Comments (2)

  1. Daniel Smith says:

    I use exactly the same convensions as yourself, including the underscore for private fields even though it feels slightly naughty!

    I tried camel casing for private fields for a while, but it felt just a little bit too weird.

    Really hope StyleCop can be integrated directly into Visual Studio one of these days.

  2. ukadc says:

    Hi Daniel,

    StyleCop was designed be run within Visual Studios directly after installation.

    Here’s a link to the Microsoft StyleCop MSDN page.