Post

.NET naming Conventions

I found this great .NET Naming Convention guide from Josh Twist at The Joy Of Code.

Reading it, I decided to put it up here, since it is so great, and it’s the naming convention that I follow myself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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, the last comment is also 100% on the spot;

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

This post is licensed under CC BY 4.0 by the author.