In the world of programming, the naming convention is a set of rules to follow as you decide what to name your identifiers such as class, variable, constant, method, etc. Consistent naming conventions can make your code more understandable and maintainable by conveying the role and purpose of the identifier in an efficient manner. This tutorial will guide you through the recommended naming conventions in C#.Net with examples.
Naming conventions in C#
Identifier | Naming Convention | Example |
---|---|---|
Class | PascalCase | public class Customer |
Interface | PascalCase, starts with ‘I’ | public interface ICustomerRepository |
Method | PascalCase | public void SaveCustomerData() |
Variable | camelCase | string customerName |
Parameter | camelCase | SetCustomerName(string customerName) |
Constant | PascalCase | public const int MaxUsers |
Enum type | PascalCase | public enum State |
Enum values | PascalCase | Alabama, Alaska, Arizona |
Property | PascalCase | public string FirstName { get; set; } |
Delegate | PascalCase, ends with ‘Handler’ or ‘Callback’ | public delegate void ErrorHandler(string errorMessage) |
Event | PascalCase, starts with ‘On’ | public event EventHandler OnDataSaved |
Namespace | PascalCase, follow directory structure | namespace MyCompany.ProjectName.Module |
General Naming Rules in C#
Here are some general rules that apply to all types of identifiers.
Capitalization Styles
In C#, there are two main capitalization styles:
- PascalCase: The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. For example,
FirstName
,LastName
. - camelCase: The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example,
firstName
,lastName
.
General Rules
- Choose descriptive names. Longer descriptive names are preferred over short cryptic names. For example,
lastName
is better thanln
. - Use alphabetic characters. Avoid using numbers at the start of identifier names. It’s allowed, but it’s not considered good practice. For example,
user1
is not recommended. - Don’t use underscores (_), hyphens (-), or any other special characters.
- Avoid using C# keywords. For example,
string
,int
,bool
etc. If you must use a keyword, prefix it with ‘@’. For example,@string
. - Do not use abbreviations or contractions as part of identifier names. For example,
num
instead ofnumber
orcnt
instead ofcount
.
C#.Net Naming Conventions for Different Types of Identifiers
Classes
Classes in C# should be named in PascalCase. The name should be descriptive, and it should typically be a noun or a noun phrase. Here’s an example:
public class Customer
{
// class members go here
}
Interfaces
Interfaces should also follow PascalCase. By convention, they should start with the letter ‘I’. Following the ‘I’, the name should describe the behavior that the interface encapsulates. Here’s an example:
public interface ICustomerRepository
{
// interface members go here
}
Methods
Method names in C# should also follow PascalCase. The name should typically be a verb or a verb phrase, and it should clearly express what the method does. For instance:
public class Customer
{
public void SaveCustomerData()
{
// method body goes here
}
}
Variables and Parameters
Variable names and method parameter names should be in camelCase. They should be descriptive and convey the purpose of the variable or parameter. For example:
public class Customer
{
private string customerName;
public void SetCustomerName(string customerName)
{
this.customerName = customerName;
}
}
Constants
Constants in C# are named using PascalCase. They should be descriptive and typically use noun phrases. Here’s an example:
public class Constants
{
public const int MaxUsers = 100;
}
Enums
Enum type and enum values are named using PascalCase. Enum type is typically a noun and enum values are typically adjectives. For example:
public enum State
{
Alabama,
Alaska,
Arizona,
// other states go here
}
Properties
Properties in C# are named using PascalCase, similar to methods. The name should typically be a noun or a noun phrase. Here’s an example:
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Delegates
Delegates in C# are named using PascalCase. They should typically end with ‘Handler’ or ‘Callback’. Here’s an example:
public delegate void ErrorHandler(string errorMessage);
Events
Events in C# are named using PascalCase. They should typically start with ‘On’. For example:
public class Customer
{
public event EventHandler OnDataSaved;
}
Namespaces
Namespaces in C# are named using PascalCase. They should typically follow the directory structure of your project. For example:
namespace MyCompany.ProjectName.Module
{
// classes, interfaces, etc. go here
}
Examples of Naming Convention in C#
Let us see an example of the naming conventions in C#.Net. We will create a Customer
class that represents a customer in a shopping system.
namespace USA.ShoppingSystem
{
public class Customer
{
private string firstName;
private string lastName;
private State customerState;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public State CustomerState
{
get { return customerState; }
set { customerState = value; }
}
public void SaveCustomerData()
{
// Save customer data here
OnDataSaved();
}
public event EventHandler OnDataSaved = delegate { };
}
public enum State
{
Alabama,
Alaska,
Arizona,
// other states go here
}
}
In this example, we have a Customer
class with three properties FirstName
, LastName
, and CustomerState
. The class also has a method SaveCustomerData
and an event OnDataSaved
. We also have an enum State
that represents a number of states in the USA. Notice how all identifiers follow the naming conventions discussed above.
Conclusion
Proper naming conventions in C#.Net ensures readability, understandability, and maintainability of your codebase. By following the standard conventions for identifiers like classes, interfaces, methods, variables, parameters, constants, enums, properties, delegates, events, and namespaces, you not only make your code easier to interpret for other developers, but you also contribute to a more organized and professional development environment.
You may also like the following C#.Net tutorials:
- C#.Net Fields Naming Conventions
- C#.NET Method Naming Conventions
- C# Constant Naming Conventions with Examples
- C# dictionary naming conventions
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…