Code Quality Help

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 I prefix (e.g., IOrderRepository, ILogger<T>).

  • Methods: PascalCase, verb or verb phrase (e.g., GetUserAsync, CalculateTotal). Exception: test methods follow the pattern MethodName_Scenario_ExpectedBehavior (see Testing section).

  • Properties: PascalCase (e.g., FirstName, IsActive).

  • Fields:

    • Public fields: PascalCase.

    • Private fields: _camelCase with 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: T for single parameter, or descriptive PascalCase prefix with T (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:

namespace Company.Project.Orders; public interface IOrderRepository { Task<Order> GetById(string id, CancellationToken cancellationToken); } public class OrderService : IOrderService { private readonly IOrderRepository _orderRepository; private readonly ILogger<OrderService> _logger; public OrderService(IOrderRepository orderRepository, ILogger<OrderService> logger) { _orderRepository = orderRepository; _logger = logger; } public Task<Order> GetOrder(string orderId, CancellationToken ct) { return _orderRepository.GetById(orderId, ct); } }
04 мая 2026