In this C#.Net tutorial, we’ll be discussing conventions for naming classes, variables, properties, members, fields, and libraries in C#. Basically, we will cover everything on C#.Net Class Naming Conventions with examples.
Here is a summary table of C# class naming conventions:
Element | Naming Convention | Example |
---|---|---|
Class | PascalCase | public class MyClass {} |
Variable | camelCase | private int myVariable; |
Property | PascalCase | public int MyProperty { get; set; } |
Member (private) | underscore + camelCase | private int _myMember; |
Member (public) | PascalCase | public int MyMember; |
Field | underscore + camelCase | private int _myField; |
Class Library (Namespace) | Company.Product.Module (PascalCase) | namespace CompanyName.ProductName.ModuleName {} |
Remember, the first letter of identifiers in PascalCase is capitalized, while in camelCase, it is lowercase. For private members and fields, we often use an underscore prefix.
Why Naming Conventions Matter
Consistency is crucial in any programming language. Good naming conventions help make your code more readable, maintainable, and easier to understand by others (and even by your future self). It’s important to stick to these conventions, especially when working in a team, as they provide a set of standards to follow.
C#.Net Class Naming Convention
Classes in C# are named using PascalCase. This means that the first letter of each word in the class name is capitalized.
Example:
public class MyClass {}
In this example, “MyClass” is the name of the class. Note that each word’s first letter is capitalized. Class names should also be nouns or noun phrases, and not verbs.
C# Class Variable Naming Convention
The naming convention for variables in C# is camelCase. This means that the first letter of the first word is lower case, but the first letter of each subsequent word is upper case.
Example:
public class MyClass
{
private int myVariable;
}
In this example, “myVariable” is the name of a variable. It starts with a lower case letter, and the first letter of each subsequent word is capitalized.
C#.Net Class Property Naming Convention
C# property names follow the same PascalCase convention as class names.
Example:
public class MyClass
{
public int MyProperty { get; set; }
}
In this example, “MyProperty” is the name of a property. Note that each word’s first letter is capitalized.
C# Class Member Naming Convention
In C#, member variables (or fields) are usually prefixed with an underscore and follow the camelCase convention.
Example:
public class MyClass
{
private int _myField;
}
In this example, “_myField” is the name of a member variable. It starts with an underscore, followed by a lowercase letter, and the first letter of each subsequent word is capitalized.
Public members of a class use PascalCase, without an underscore:
public class MyClass
{
public int MyField;
}
C#.Net Class Field Naming Convention
Class fields are typically named using camelCase, often with an underscore prefix. This helps distinguish them from local variables.
Example:
public class MyClass
{
private int _myField;
}
In this example, “_myField” is a private field. It’s prefixed with an underscore and uses camelCase.
C# Class Library Naming Convention
The naming convention for a class library in C# follows the Company.Product.Module pattern and is PascalCase.
Example:
namespace CompanyName.ProductName.ModuleName
{
// Code goes here...
}
In this example, “CompanyName.ProductName.ModuleName” is the name of a class library. Each segment is capitalized, and each word within those segments is also capitalized.
C#.Net Class Naming Conventions Example
Here is a complete example that demonstrates all of these naming conventions in a C# class:
// Namespace (Class library)
namespace Company.Product.Module
{
// Class name
public class Customer
{
// Private field (member variable)
private int _id;
// Public field (not commonly used)
public string PublicField;
// Property
public string Name { get; set; }
// Variable inside a method (local variable)
public void SetId(int newId)
{
int myVariable = newId;
_id = myVariable;
}
// Accessing the private field through a property
public int Id
{
get { return _id; }
private set { _id = value; }
}
}
}
In this example:
Company.Product.Module
is the namespace (class library).Customer
is the class._id
is a private field (also a member variable).PublicField
is a public field (not commonly used, but included for completeness).Name
is a property.newId
andmyVariable
are variables within theSetId
method.Id
is a property that encapsulates the_id
field, providing a way to get its value and privately set it.
Conclusion
Adhering to the established C#.Net Class Naming Conventions significantly enhances the readability, maintainability, and overall quality of your code. By employing a consistent naming strategy, you aid others in understanding your code, whether they’re colleagues working on the same project or other developers who may come across your work in the future.
You may also like:
- C#.NET Variable Naming Conventions
- C# dictionary naming conventions
- C#.Net array naming conventions with examples
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…