Writing clean, readable, and maintainable code is essential for long-term success in any software project. For developers working with C#, following consistent coding standards not only improves collaboration but also reduces bugs, enhances readability, and ensures scalability.
![coding-standards]()
This article covers the most widely accepted C# coding standards and best practices that every .NET developer should follow.
1. Naming Conventions
1.1 PascalCase
Used for:
Class names
Methods
Properties
Namespaces
public class CustomerService
{
public void ProcessOrder() { }
public string CustomerName { get; set; }
}
1.2 camelCase
Used for:
private int _orderCount;
public void AddCustomer(string customerName)
{
int totalOrders = 0;
}
1.3 UPPER_CASE
Used for:
public const int MAX_RETRY_COUNT = 3;
2. Commenting and Documentation
2.1 XML Comments
Use XML comments for methods, classes, and public APIs.
/// <summary>
/// Calculates the total price including tax.
/// </summary>
/// <param name="amount">Base amount.</param>
/// <param name="tax">Tax percentage.</param>
/// <returns>Total price with tax included.</returns>
public decimal CalculatePrice(decimal amount, decimal tax) { }
2.2 Inline Comments
Use inline comments only when logic is not easily readable.
3. Coding Style Guidelines
3.1 Use var Wisely
Use var when the type is obvious.
✔ Good
var customer = new Customer();
✘ Bad
var result = GetData(); // unclear type
3.2 One Class per File
Keep file organization clean.
3.3 Keep Methods Short
Each method should ideally do one task.
4. Error Handling Practices
4.1 Use Try-Catch Only for Exceptional Cases
Avoid writing empty catch blocks.
try
{
SaveFile();
}
catch (IOException ex)
{
_logger.LogError(ex, "File saving failed");
}
4.2 Throw Meaningful Exceptions
throw new ArgumentNullException(nameof(customerId));
5. SOLID Principles Overview
S — Single Responsibility Principle
A class should have one reason to change.
O — Open/Closed Principle
Code should be open for extension, closed for modification.
L — Liskov Substitution Principle
Child classes should not break parent behavior.
I — Interface Segregation Principle
Prefer small, specific interfaces.
D — Dependency Inversion Principle
Depend on abstractions, not concrete classes.
6. Use Async Programming Properly
Use async/await for non-blocking IO operations.
public async Task<List<Customer>> GetCustomersAsync()
{
return await _dbContext.Customers.ToListAsync();
}
Avoid using .Result and .Wait() as they may cause deadlocks.
7. Avoid Hardcoding Values
Use configuration or constants instead.
✔ Good
var apiUrl = _config["ApiSettings:BaseUrl"];
✘ Bad
var apiUrl = "https://example.com/api";
8. Organize Your Project Structure
A clean folder structure improves maintainability:
/Controllers
/Services
/Repositories
/Models
/DTOs
/Interfaces
9. Use Dependency Injection
Built-in DI in .NET promotes cleaner architecture.
services.AddScoped<ICustomerService, CustomerService>();
10. Write Unit Tests
Good coding standards include testing.
[TestMethod]
public void AddOrder_ValidOrder_ShouldReturnTrue()
{
// Arrange
// Act
// Assert
}