- Always utilize the latest C# 12 features such as primary constructors, collection expressions, and default interface methods where applicable.
- Enable and use nullable reference types to avoid null reference exceptions and improve code safety.
- Use async/await for asynchronous programming. Avoid blocking calls and ensure proper exception handling in asynchronous methods.
- Leverage the built-in dependency injection framework for managing dependencies. Use constructor injection as the preferred method.
- When building web applications, consider using minimal APIs for simple HTTP services to reduce boilerplate code and improve performance.
- Use Entity Framework Core for data access. Follow best practices for performance, such as using AsNoTracking for read-only queries and batching multiple operations.
- Implement structured logging using Microsoft.Extensions.Logging. Ensure that all critical operations are logged and consider integrating with monitoring tools like Application Insights.
- Follow security best practices, including using HTTPS, validating user inputs, and implementing proper authentication and authorization mechanisms.
- Adhere to the .NET coding conventions, such as using PascalCase for class names and camelCase for local variables. Use consistent naming patterns and keep methods short and focused.
- Write unit tests for all critical components using xUnit or NUnit. Ensure high code coverage and use mocking frameworks like Moq for dependencies.
- Regularly profile and optimize your application for performance. Use tools like dotnet-counters and dotnet-trace to identify bottlenecks.
- Write XML documentation comments for public APIs and ensure that all methods and classes are well-documented.
- Use LINQ for querying collections and databases. Optimize LINQ queries for performance and readability.
- Utilize global usings to simplify and clean up your code files by reducing repetitive using directives.
- Use records for defining immutable data structures, especially when working with data transfer objects or value objects.
- Use the built-in configuration system for managing application settings. Support multiple environments and use environment variables for sensitive data.
- Enable and configure .NET analyzers to enforce code quality and best practices during development.