Essential naming guidelines for C# developers

Naming conventions are essential to ensure that code is readable, consistent, and easy to maintain. In C# and .NET, following the recommended naming conventions improves clarity, facilitates collaboration among developers, and avoids ambiguities in the code. This article explores the main naming conventions for classes, methods, properties, variables, interfaces, and enumerations, offering practical examples and discussing the importance of good practices.

The importance of following conventions

Following naming standards is essential in any programming language, but in the context of C# and .NET, these conventions are more than recommended — they are almost a community standard. The main reasons for following these conventions include:

  • Readability: well-named code is easier to read and understand, even by people who were not involved in the initial development;

  • Maintainability: maintaining and updating the code becomes simpler, as the names of variables and functions already indicate their purpose;

  • Consistency: following a consistent pattern throughout the project improves code quality and makes peer reviews easier;

  • Reusability and extensibility: well-structured and properly named code tends to be more modular and easier to expand.

Now, let’s examine the main naming conventions in C# and .NET.

Naming classes

Starting with classes, in C#, they follow the PascalCase convention, where each word in the name starts with an uppercase letter. The same applies to public properties. A class name and its properties should be clear and descriptive regarding their purpose:

				
					public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
}

				
			

If any class property is not in PascalCase, a warning will be issued, suggesting that the naming be corrected.

Examples:

  • ✅ Correct: Customer, PaymentMethod, Order

  • ❌ Incorrect: customer, payment_method, order

Naming methods

Methods also use PascalCase. The method name should indicate what it does, often starting with a verb in the infinitive. Methods should be named to clearly reflect their function.

				
					public class Calculator
{
    public int SumValues(int firstValue, int secondValue)
    {
        return firstValue + secondValue;
    }
}

				
			

Examples:

  • ✅ Correct: CalculateDiscount, AddItem, RemoveCustomer

  • ❌ Incorrect: sumValues, sum_values, SUMVALUES

Naming local variables and parameters

For local variables and method parameters, the convention is to use camelCase. That is, the first letter will be lowercase, and the first letter of each subsequent word will be uppercase. The name must be descriptive.

				
					public int CalculateDiscount(int totalValue, int discount)
{
    int finalValue = totalValue - discount;
    return finalValue;
}

				
			

Examples:

✅ Correct:

  • AddItem(OrderItem item)
  • GetCustomers(string name, string document)
  • string firstName
  • DateTime lastAccessTime;

❌ Incorrect:

  • CalculateDiscount(int total_value, int discount)
  • CalculateDiscount(int TOTALVALUE, int DISCOUNT)
  • int final_value
  • int FINALVALUE

Naming interfaces

Interfaces in C# also use PascalCase and may contain an “I” at the beginning of the name to indicate that it is an interface. The interface name should describe an action or behavior.

				
					public interface ICommand
{
    void Execute();
}

				
			

Naming enums

Enumerations also follow the PascalCase convention, where the enum name and each of its values start with an uppercase letter. Microsoft recommends that values be clear and descriptive, accurately representing states or categories with nouns or adjectives.

In the .NET community, it is common to use the prefix “E” before the enum name (such as EStatus) to visually identify it as an enum type. This practice is especially helpful in projects with many entities and classes.

				
					public enum Status
{
    Pending,
    Sent,
    Delivered,
    Canceled
}

				
			

Examples:

✅ Correct:

  • public enum PaymentMethod
  • public enum OrderStatus

❌ Incorrect:

  • public enum paymentMethod
  • public enum ORDER_STATUS

 

Best practices in naming

Avoid abbreviations and ambiguous names

Avoiding abbreviations and names that do not explain the purpose is essential. Even though shorter names might seem convenient, clarity should be prioritized. Explicit names make code easier to understand, even if they are a bit longer.

  • ✅ Correct: GetCustomerById, ProcessBarCode

  • ❌ Incorrect: Get, GetCustById

Clear names help quickly identify the function or objective of the method or variable, making the code more intuitive and accessible for developers working on the same project.

Avoid identifiers that conflict with keywords

It is recommended to avoid using identifiers that conflict with keywords of widely used programming languages. According to CLS Rule 4 (Common Language Specification), all compatible languages must provide a mechanism that allows access to named items that use a keyword as an identifier. In C#, the “@” symbol is used as an escape mechanism in this case.

				
					string @string;

// or

public class Example
{
    public void @class()
    {
        // Code
    }
}

				
			

Although it is possible to use “@” to avoid keyword conflicts, a good practice is to avoid using reserved words as identifiers whenever possible. Methods or variables using the “@” escape can be confusing and harder to maintain, in addition to impairing code readability.

Conclusion

Following naming conventions in C# and .NET is an essential practice for creating clean, readable, and maintainable code. Proper use of PascalCase and camelCase, avoiding abbreviations, ensuring names are clear and descriptive, and avoiding conflicts with keywords are practices that should be strictly followed. Maintaining these good practices results in more professional code and, most importantly, code that is easier to collaborate on and expand in the long term.