CSharp Coding Guidelines
CSharp Coding Guidelines
Contents
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2. Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3. Coding Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.1. Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4. Variables & Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5. Language guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.1. Catch exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.2. String data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.3. Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4. && and || operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.5. LINQ queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.6. Using Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
6. Style guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
7. Comment style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
8. Layout conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9. Securing resource access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
10. Formatters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
10.1. EditorConfig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
10.2. dotnet-format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
11. Linter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
11.1. StyleCop Analyzers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
11.2. SonarLint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
12. Type Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
12.1. Roslyn Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
13. Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1. Introduction
This document describes rules and recommendations for developing applications and class
libraries using the C Sharp Language. The goal is to define guidelines to enforce consistent style
and formatting and help developers avoid common pitfalls and mistakes. Coding conventions
are essential for maintaining code readability, consistency, and collaboration within a develop-
ment team.
2. Naming Conventions
Legend:
• "c" = camel Case
• "P" = Pascal Case
1
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
3. Coding Style
1. Do use Pascal Casing for class names and method names.
2
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
3
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
7. Do use predefined type names instead of system type names like Int16, Single, UInt64, etc
4
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
9. Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
}
}
12. Do declare all member variables at the top of a class, with static variables at the very top.
5
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
6
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
3.1. Formatting
1. Never declare more than 1 namespace per file.
2. Avoid putting multiple classes in a single file.
3. Always place curly braces ({ and }) on a new line.
4. Always use curly braces ({ and }) in conditional statements.
5. Always use a Tab & Indention size of 4.
6. Declare each variable independently – not in the same statement.
7. Place namespace “using” statements together at the top of file. Group .NET namespaces
above custom namespaces.
8. Group internal class implementation by type in the following order:
• Member variables.
• Constructors & Finalizers.
• Nested Enums, Structs, and Classes.
• Properties
• Methods
9. Sequence declarations within type groups based upon access modifier and visibility.
• Public
• Protected
• Internal
• Private
10. Segregate interface Implementation by using region statements.
11. Append folder-name to namespace for source files within sub-folders.
12. Recursively indent all code blocks contained within braces.
13. Use white space (CR/LF, Tabs, etc) liberally to separate and organize code.
7
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
1. Only declare member variables as private. Use properties to provide access to them with
public, protected, or internal access modifiers.
2. Try to use int for any non-fractional numeric values that will fit the int datatype - even
variables for non negative numbers.
3. Only use long for variables potentially containing values too large for an int.
4. Try to use double for fractional numbers to ensure decimal precision in calculations.
5. Only use float for fractional numbers that will not fit double or decimal.
6. Avoid using float unless you fully understand the implications upon any calculations.
7. Try to use decimal when fractional numbers must be rounded to a fixed precision for calcu-
lations. Typically this will involve money.
8. Avoid using sbyte, short, uint, and ulong unless it is for interop (P/Invoke) with native
libraries.
5. Language guidelines
5.3. Delegates
1. Use Func<> and Action<> instead of defining delegate types.
8
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
9
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
namespace CoolStuff.AwesomeFeature
{
}
6. Style guidelines
Use the following format for code samples:
• Use tabs for indentation.
• Align code consistently to improve readability.
• Limit lines to 65 characters to enhance code readability on docs, especially on mobile screens.
• Break long statements into multiple lines to improve clarity.
• Use the “Allman” style for braces: open and closing brace its own new line. Braces line up
with current indentation level.
• Line breaks should occur before binary operators, if necessary
7. Comment style
• Use single-line comments (“//”) for brief explanations.
• Avoid multi-line comments (“/* */”) for longer explanations. Comments aren’t localized.
Instead, longer explanations are in the companion article.
• For describing methods, classes, fields, and all public members use XML comments.
• Place the comment on a separate line, not at the end of a line of code.
• Begin comment text with an uppercase letter.
• End comment text with a period.
• Insert one space between the comment delimiter (“//”) and the comment text.
8. Layout conventions
Good layout uses formatting to emphasize the structure of your code and to make the code
easier to read.
• Use the default Code Editor settings
• Smart indenting,
• Four-character indents,
• Tabs saved as spaces
• Write only one statement per line.
• Write only one declaration per line.
10
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
• If continuation lines aren’t indented automatically, indent them one tab stop (four spaces).
• Add at least one blank line between method definitions and property definitions.
• Use parentheses to make clauses in an expression apparent, as shown in the following code.
10. Formatters
10.1. EditorConfig
A powerful tool built into Visual Studio that helps standardize code formatting
across teams. You can define rules for indentation, spacing, and more in a .editorconfig file.
10.2. dotnet-format
: A .NET tool that applies consistent code formatting based on your project’s .editorconfig
settings. It can be run from the command line.
11. Linter
11.2. SonarLint
A powerful linting tool that provides in-depth static code analysis to catch potential
bugs, code smells, and security vulnerabilities in C Sharp. It supports integration with Vi-
sual Studio.
11
Hue Learn, Wise Work, DNAi World C# Coding Guidelines
pilation process, giving them deep access to code syntax, semantics, and project
metadata.
Key Features of Roslyn Analyzer:
• Static Code Analysis:
Roslyn Analyzers analyze your code at compile-time to catch potential issues be-
fore the code is run. They provide warnings, errors, and suggestions in the IDE (e.g., Visual
Studio, Visual Studio Code).
• Custom Rules:
You can create your own custom analyzers to enforce specific coding guidelines or
company standards, beyond what is provided by default. For example, you can enforce
naming conventions, code patterns, or deprecations.
• Code Fixes:
Analyzers can suggest automatic code fixes. For example, if an analyzer detects that a
naming convention isn’t followed, it can suggest a fix and allow you to apply it with a single
click in the IDE.
• Real-time Feedback:
As you write code, Roslyn Analyzers provide real-time feedback in the form of squiggly lines,
error messages, or warnings, helping you catch issues early.
• Integration with Build Pipelines:
Roslyn Analyzers can be integrated into build pipelines (like CI/CD) to enforce cod-
ing rules across teams. They can fail a build if critical violations are detected.
• Deep Language Understanding:
Since Roslyn is the compiler platform for .NET, the analyzers have full access to the code’s
syntax tree, semantic model, and project metadata. This allows them to perform sophisticated
analysis and provide more meaningful diagnostics compared to general-purpose linters.
13. Conclusion
Adhering to these coding guidelines is vital for producing clean, maintainable, and efficient
code. By following conventions such as naming standards, proper code structure, and adhering
to best practices, teams can ensure that the code is both readable and consistent across projects
and create software that is easier to maintain and scale over time.
12