Use PascalCase.
Name the class explicitly and meaningfully.
public class CustomerManager
{
// Code
}
Use PascalCase.
Name the method based on its action.
public void CalculateTotal ( )
{
// Code
}
1.3 Variable and Field Naming
Use camelCase for local variables and parameters.
Use _camelCase for private fields.
private string _customerName ;
public void SetCustomerName ( string customerName )
{
_customerName = customerName ;
}
Use PascalCase.
Prefix the declaration with const.
public const int MaxRetryCount = 5 ;
public interface IRepository
{
void Save ( ) ;
}
public enum OrderStatus
{
Pending ,
Shipped ,
Delivered
}
2. Comments and Documentation
2.1 Using XML Comments
Always document public classes and methods.
/// <summary>
/// Customer management class.
/// </summary>
public class CustomerManager
{
/// <summary>
/// Adds a customer.
/// </summary>
/// <param name="customer">Customer object to add.</param>
public void AddCustomer ( Customer customer )
{
// Add code
}
}
2.2 Code Comments
Use // comments to explain complex code blocks.
// Check if the customer already exists
if ( customerList . Contains ( customer ) )
{
return ;
}
Use a framework like NUnit or XUnit.
Name tests following [MethodUnderTest]_Condition_ExpectedResult.
using Xunit ;
public class CustomerManagerTests
{
[ Fact ]
public void AddCustomer_WithValidCustomer_ShouldAddSuccessfully ( )
{
// Arrange
var customerManager = new CustomerManager ( ) ;
var customer = new Customer ( "John Doe" ) ;
// Act
customerManager . AddCustomer ( customer ) ;
// Assert
Assert . Contains ( customer , customerManager . GetAllCustomers ( ) ) ;
}
}