C# Reference ยท .NET, Tools and Concepts
C# coding conventions
Reference for common C# coding conventions: naming, layout, var, expression bodies, null handling, async naming and how to enforce style with .editorconfig.
C# has no official style police, but nearly every team follows the same set of conventions, popularized by the .NET runtime team and the Visual Studio defaults. Following them makes your code look familiar to every other C# developer and lets analyzers and formatters work without arguments.
This page collects the conventions that matter most: naming, layout, language choices and how to enforce them automatically. Treat them as defaults; a team may agree on different rules, and consistency inside one codebase beats any single rule.
namespace Shop.Orders; public sealed class OrderService(IOrderStore store) { private readonly List<string> log = []; public async Task<Order?> FindAsync(int orderId, CancellationToken cancellationToken) => await store.GetAsync(orderId, cancellationToken); }
Try it
The first example is written in the conventional style. Compare each declaration with the naming table below.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Modern syntax often removes noise. The same logic is shown below in an older and a modern shape, and both print the same result.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
Naming
| Item | Style | Example |
|---|---|---|
| Namespace, class, struct, record, enum, delegate | PascalCase | OrderService |
| Interface | I + PascalCase |
IOrderStore |
| Method, property, event, public field, constant | PascalCase | GetTotal, MaxLines |
| Parameter, local variable | camelCase | orderId |
| Private field | camelCase (some teams use _camelCase) |
store, _store |
| Generic type parameter | T or T + PascalCase |
T, TKey, TResult |
| Async method | ends with Async |
LoadAsync |
| Attribute class | ends with Attribute |
RequiredAttribute |
| Acronyms | PascalCase for 3+ letters, upper for 2 | HtmlParser, IOStream, Id |
Pick one private-field style (name or _name) per codebase and keep it.
Layout
| Rule | Details |
|---|---|
| Indentation | 4 spaces, no tabs. |
| Braces | On their own line (Allman style); always use braces, even for one statement. |
| Line length | Keep lines readable, around 100 to 120 characters. |
| One statement and one declaration per line | int a, b; is discouraged. |
using directives |
Outside the namespace, System first, or let global usings handle the common ones. |
| Namespaces | File-scoped (namespace Shop;) saves one indent level. |
| Members order | Constants and fields, constructors, properties, methods, nested types. |
Usage notes
var: use it when the type is obvious from the right side (var list = new List<int>();). Spell the type out when it is not (int count = GetCount();).- Nullable reference types on: annotate honestly with
?. Do not silence warnings with!unless you can prove a value is non-null. - Guard clauses over deep nesting. Return early on invalid input, and use
ArgumentNullException.ThrowIfNull(value)for public API arguments. - Prefer
string.IsNullOrWhiteSpace,TryParse, pattern matching (is not null,is { Length: > 0 }) and string interpolation over older equivalents. - Collections: accept the narrowest interface (
IEnumerable<T>,IReadOnlyList<T>), return concrete or read-only types, and use collection expressions ([1, 2, 3]). asyncall the way: do not call.Resultor.Wait(); pass aCancellationTokenthrough, and name the method...Async.- Do not catch
Exceptionto hide problems. Catch specific types, log or rethrow withthrow;(notthrow ex;, which resets the stack trace). - Comments explain why, not what. Use
///XML documentation on public APIs. - Dispose what you own:
using var stream = File.OpenRead(path);. - Enforce it automatically. Put the rules in an
.editorconfigfile and enable analyzers; the build can then fail on violations.
# .editorconfig root = true [*.cs] indent_style = space indent_size = 4 csharp_style_namespace_declarations = file_scoped:warning csharp_style_var_for_built_in_types = false:suggestion dotnet_naming_rule.private_fields_camel.severity = suggestion dotnet_style_prefer_collection_expression = true:suggestion
dotnet format applies the style rules to existing files. See Analyzers and code style.