Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Naming Conventions in C#
Naming conventions in C# help maintain code readability and consistency across projects. Following standardized naming patterns makes your code more professional and easier to understand for other developers.
Class Naming Conventions
A class definition starts with the class keyword followed by the class name, enclosed by curly braces. The following conventions apply to class names.
Pascal Casing
Class names should use PascalCasing, where the first letter of each word is capitalized −
public class EmployeeDetails { }
public class BankAccount { }
public class CustomerOrderHistory { }
Noun or Noun Phrases
Class names should be nouns or noun phrases that clearly describe what the class represents −
public class Employee { }
public class DatabaseConnection { }
public class PaymentProcessor { }
Method Naming Conventions
Methods should use PascalCasing and typically start with verbs that describe the action performed −
using System;
class Calculator {
public int AddNumbers(int a, int b) {
return a + b;
}
public void DisplayResult(int result) {
Console.WriteLine("Result: " + result);
}
public bool ValidateInput(int number) {
return number > 0;
}
}
class Program {
public static void Main() {
Calculator calc = new Calculator();
int sum = calc.AddNumbers(10, 20);
calc.DisplayResult(sum);
bool isValid = calc.ValidateInput(sum);
Console.WriteLine("Is valid: " + isValid);
}
}
The output of the above code is −
Result: 30 Is valid: True
Variable and Field Naming Conventions
Different types of variables follow specific casing conventions −
| Type | Convention | Example |
|---|---|---|
| Local variables | camelCasing | firstName, totalAmount |
| Method parameters | camelCasing | userName, itemCount |
| Public fields/properties | PascalCasing | Name, Age, IsActive |
| Private fields | _camelCasing | _connectionString, _maxRetries |
| Constants | PascalCasing | MaxValue, DefaultTimeout |
Example
using System;
class Student {
// Private fields with underscore prefix
private string _firstName;
private int _studentId;
// Public properties with PascalCasing
public string FirstName {
get { return _firstName; }
set { _firstName = value; }
}
public int StudentId {
get { return _studentId; }
set { _studentId = value; }
}
// Constant with PascalCasing
public const int MaxCourses = 8;
// Method with camelCasing parameters
public void SetStudentInfo(string firstName, int studentId) {
// Local variables with camelCasing
string trimmedName = firstName.Trim();
int validatedId = studentId > 0 ? studentId : 1;
FirstName = trimmedName;
StudentId = validatedId;
}
public void DisplayInfo() {
Console.WriteLine($"Student: {FirstName}, ID: {StudentId}");
Console.WriteLine($"Max courses allowed: {MaxCourses}");
}
}
class Program {
public static void Main() {
Student student = new Student();
student.SetStudentInfo("John Doe", 12345);
student.DisplayInfo();
}
}
The output of the above code is −
Student: John Doe, ID: 12345 Max courses allowed: 8
Identifier Rules
Identifiers are names used to identify classes, variables, methods, or any user-defined items. The following rules apply to all identifiers −
-
Must begin with a letter or underscore, followed by letters, digits (0-9), or underscores.
-
Cannot start with a digit.
-
Cannot contain spaces or special symbols like
? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / \. Only underscores are allowed. -
Cannot be C# keywords like
class,int,string, etc. -
Are case-sensitive:
Nameandnameare different identifiers.
Interface and Namespace Conventions
Interfaces should start with I followed by PascalCasing, and namespaces should use PascalCasing −
namespace CompanyName.ProjectName.ModuleName {
public interface IPaymentProcessor {
void ProcessPayment(decimal amount);
}
public interface IDataRepository {
void SaveData(object data);
}
}
Conclusion
Following C# naming conventions improves code readability and maintainability. Use PascalCasing for classes and methods, camelCasing for local variables and parameters, and prefix private fields with underscore. Consistent naming makes your code professional and easier to collaborate on with other developers.
