Naming conventions in C# are essential for writing clean, readable, and maintainable code. They provide consistency and help developers understand the purpose and scope of different elements in a program.
Use Pascal Case for naming classes, interfaces, methods, properties, and public members. The first letter of each word is capitalized.
public class CustomerOrder
{
public void ProcessPayment() { }
public string CustomerName { get; set; }
}
Use Camel Case for naming private fields and local variables. The first letter is lowercase, and subsequent words start with an uppercase letter.
public class Order
{
private int orderNumber;
public void CalculateTotal()
{
decimal totalAmount = 0;
// ...
}
}
Prefix interface names with the letter 'I' followed by Pascal Case.
public interface IPayable
{
void Pay();
}
Use all uppercase letters with underscores between words for constants and static readonly fields.
public class Configuration
{
public const string DEFAULT_CONNECTION_STRING = "...";
public static readonly int MAX_RETRY_ATTEMPTS = 3;
}
Use Pascal Case for namespaces, and separate logical components with dots.
namespace CompanyName.ProductName.Module.Submodule
{
// ...
}
Understanding naming conventions is crucial for writing clean C# code. To further improve your C# skills, explore these related topics:
By following these naming conventions, you'll create more readable and maintainable C# code. Consistent naming practices help teams collaborate effectively and make it easier for developers to understand and work with each other's code.