C# Naming Conventions
Namespaces: PascalCase, matching the project structure (e.g.,
Company.Project.Feature).Classes and Structs: PascalCase, noun or noun phrase (e.g.,
OrderService,CustomerInfo).Interfaces: PascalCase with an
Iprefix (e.g.,IOrderRepository,ILogger<T>).Methods: PascalCase, verb or verb phrase (e.g.,
GetUserAsync,CalculateTotal). Exception: test methods follow the patternMethodName_Scenario_ExpectedBehavior(see Testing section).Properties: PascalCase (e.g.,
FirstName,IsActive).Fields:
Public fields: PascalCase.
Private fields:
_camelCasewith a leading underscore (e.g.,_logger,_orderRepository). This differentiates them from local variables and parameters.Static private fields: same
_camelCase.
Local variables and method parameters: camelCase (e.g.,
userName,cancellationToken).Constants: PascalCase (e.g.,
MaxRetryCount). For private constants, same rule.Generic type parameters:
Tfor single parameter, or descriptive PascalCase prefix withT(e.g.,TResult,TEntity).Async methods: do not suffix with
Async(e.g.,GetData).Inlined variables: prefer to return results directly, rather than using inlined variables.
async/await: Prefer returning results directly, rather than using
async/await.
Examples: