A summary of the official Microsoft C# coding conventions, based on Microsoft’s documentation.
- PascalCase for class, property, method, and namespace names
public class MyExampleClass { }
- camelCase for local variables and method parameters
int itemCount;
- Private fields:
camelCase
(underscore prefix is not allowed)
private int itemCount;
- Interface names: Prefix with
I
public interface IMyInterface { }
- No Hungarian notation or type in variable names.
- Use descriptive, meaningful names.
- Indentation: 4 spaces per indentation level (no tabs).
- Curly braces:
- For types and methods: Opening brace on a new line
public class MyClass { }
- For other blocks (if, else, etc.): Opening brace on a new line
if (true) { }
- For types and methods: Opening brace on a new line
- Blank lines: Separate logical sections with blank lines for readability.
- Line length: Prefer lines under 120 characters.
- Place
using
directives at the top of the file, outside the namespace. - Group similar code elements (fields, constructors, methods).
- Order elements by accessibility:
public
,internal
,protected
,private
.
- Prefer
var
when the type is obvious or unimportant. - Otherwise, use explicit type names for clarity.
- Use expression-bodied members for simple property or method definitions.
- Prefer modern C# features (pattern matching, interpolated strings, etc.) where appropriate.
- Use XML documentation comments (
///
) for public APIs. - Write clear, concise comments only where necessary.
- Avoid stating the obvious.
- Use verbatim strings (
@""
) for file paths and regular expressions. - Prefer string interpolation (
$"Value is {value}"
) over concatenation.
- Use exceptions for error handling, not return codes.
- Catch only exceptions you can handle.
- Avoid empty catch blocks (never swallow exceptions silently).
- Prefer auto-properties for simple property definitions.
- Use collection and object initializers for clarity.
- Avoid regions except for generated or extremely large code files.
This summary is based on the official Microsoft documentation and is intended as a quick reference.