rules/csharp/coding-style.md
This file extends common/coding-style.md with C#-specific content.
record or record struct for immutable value-like modelsclass for entities or types with identity and lifecycleinterface for service boundaries and abstractionsdynamic in application code; prefer generics or explicit modelspublic sealed record UserDto(Guid Id, string Email);
public interface IUserRepository
{
Task<UserDto?> FindByIdAsync(Guid id, CancellationToken cancellationToken);
}
init setters, constructor parameters, and immutable collections for shared statepublic sealed record UserProfile(string Name, string Email);
public static UserProfile Rename(UserProfile profile, string name) =>
profile with { Name = name };
async/await over blocking calls like .Result or .Wait()CancellationToken through public async APIspublic async Task<Order> LoadOrderAsync(
Guid orderId,
CancellationToken cancellationToken)
{
try
{
return await repository.FindAsync(orderId, cancellationToken)
?? throw new InvalidOperationException($"Order {orderId} was not found.");
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to load order {OrderId}", orderId);
throw;
}
}
dotnet format for formatting and analyzer fixesusing directives organized and remove unused imports