Start Coding

Topics

C# Naming Conventions

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.

General Rules

  • Use meaningful and descriptive names
  • Avoid abbreviations unless they are widely known
  • Don't use Hungarian notation
  • Be consistent with your naming style

Pascal Case

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; }
}
    

Camel Case

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;
        // ...
    }
}
    

Interface Naming

Prefix interface names with the letter 'I' followed by Pascal Case.


public interface IPayable
{
    void Pay();
}
    

Constants and Static Readonly Fields

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;
}
    

Namespace Naming

Use Pascal Case for namespaces, and separate logical components with dots.


namespace CompanyName.ProductName.Module.Submodule
{
    // ...
}
    

Best Practices

  • Avoid using numbers at the end of variable names
  • Don't use reserved keywords as names
  • Use noun or noun phrases for class names
  • Use verb or verb phrases for method names
  • Boolean variables and properties should start with "Is", "Has", or "Can"

Related Concepts

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.