C# Coding Standards and Naming Conventions
Sources:
Overview
About
This note is about …
Object Name | Notation | Length | Plural | Prefix | Suffix | Abbreviation | Char Mask | Underscores |
---|---|---|---|---|---|---|---|---|
Namespace name | PascalCase | 128 | Yes | Yes | No | No | [A-z][0-9] | No |
Class name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No |
Constructor name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No |
Method name | PascalCase | 128 | Yes | No | No | No | [A-z][0-9] | No |
Method arguments | camelCase | 128 | Yes | No | No | Yes | [A-z][0-9] | No |
Local variables | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Constants name | PascalCase | 50 | No | No | No | No | [A-z][0-9] | No |
Field name Public | PascalCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Field name Private | _camelCase | 50 | Yes | No | No | Yes | _[A-z][0-9] | Yes |
Properties name | PascalCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
Delegate name | PascalCase | 128 | No | No | Yes | Yes | [A-z] | No |
Enum type name | PascalCase | 128 | Yes | No | No | No | [A-z] | No |
1. Do use PascalCasing for class names and method names:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
2. Do use camelCasing for method arguments and local variables:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
3. Do not use Hungarian notation or any other type identification in identifiers
Why: consistent with the Microsoft’s .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general, you want to avoid type indicators in any identifier.
4. Do not use Screaming Caps for constants or readonly variables:
Why: consistent with the Microsoft’s .NET Framework. Caps grab too much attention.
5. Use meaningful names for variables. The following example uses seattleCustomers for customers who are located in Seattle:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
6. Avoid using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri.
Why: consistent with the Microsoft’s .NET Framework and prevents inconsistent abbreviations.
7. Do use PascalCasing or camelCasing (Depending on the identifier type) for abbreviations 3 characters or more (2 chars are both uppercase when PascalCasing is appropriate or inside the identifier).:
Why: consistent with the Microsoft’s .NET Framework. Caps would grab visually too much attention.
8. Do not use Underscores in identifiers. Exception: you can prefix private fields with an underscore:
Why: consistent with the Microsoft’s .NET Framework and makes code more natural to read (without ‘slur’). Also avoids underline stress (inability to see underline).
9. Do use predefined type names (C# aliases) like int
, float
, string
for local, parameter and member declarations. Do use .NET Framework names like Int32
, Single
, String
when accessing the type’s static members like Int32.TryParse
or String.Join
.
Why: consistent with the Microsoft’s .NET Framework and makes code more natural to read.
10. Do use implicit type var for local variable declarations. Exception: primitive types (int, string, double, etc) use predefined names.
Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.
11. Do use noun or noun phrases to name a class.
Why: consistent with the Microsoft’s .NET Framework and easy to remember.
12. Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
Why: consistent with the Microsoft’s .NET Framework.
13. Do name source files according to their main classes. Exception: file names with partial classes reflect their source or purpose, e.g. designer, generated, etc.
Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.
14. Do organize namespaces with a clearly defined structure:
Why: consistent with the Microsoft’s .NET Framework. Maintains good organization of your code base.
15. Do vertically align curly brackets:
Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.
16. Do declare all member variables at the top of a class, with static variables at the very top.
Why: generally accepted practice that prevents the need to hunt for variable declarations.
17. Do use singular names for enums. Exception: bit field enums.
Why: consistent with the Microsoft’s .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise ‘OR’).
18. Do not explicitly specify a type of an enum or values of enums (except bit fields):
Why: can create confusion when relying on actual types and values.
19. Do not use an “Enum” suffix in enum type names:
Why: consistent with the Microsoft’s .NET Framework and consistent with prior rule of no type indicators in identifiers.
20. Do not use “Flag” or “Flags” suffixes in enum type names:
Why: consistent with the Microsoft’s .NET Framework and consistent with prior rule of no type indicators in identifiers.
21. Do use suffix EventArgs at creation of the new classes comprising the information on event:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
22. Do name event handlers (delegates used as types of events) with the “EventHandler” suffix, as shown in the following example:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
23. Do not create names of parameters in methods (or constructors) which differ only by the register:
Why: consistent with the Microsoft’s .NET Framework and easy to read, and also excludes possibility of occurrence of conflict situations.
24. Do use two parameters named sender and e in event handlers. The sender parameter represents the object that raised the event. The sender parameter is typically of type object, even if it is possible to employ a more specific type.
Why: consistent with the Microsoft’s .NET Framework and consistent with prior rule of no type indicators in identifiers.
25. Do use suffix Exception at creation of the new classes comprising the information on exception:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
26. Do use prefix Any, Is, Have or similar keywords for boolean identifier:
Why: consistent with the Microsoft’s .NET Framework and easy to read.
27. Use Named Arguments in method calls:
When calling a method, arguments are passed with the parameter name followed by a colon and a value.
Why: consistent with the Microsoft’s .NET Framework and easy to read. In Named Arguments, we do not need to pass the parameters in order as defined on method definition, so we can pass the arguments in any order on method calling.
Official Reference
- MSDN General Naming Conventions
- DoFactory C# Coding Standards and Naming Conventions
- MSDN Naming Guidelines
- MSDN Framework Design Guidelines
- Common C# Coding Conventions
- Github C# Coding Style
Appendix
Note created on 2024-05-09 and last modified on 2024-05-09.
Backlinks
(c) No Clocks, LLC | 2024