§ 4 — The C# Coding Standard
Classes
Naming
Classes that represent services or brokers in a Standard-Compliant architecture should represent the type of class in their naming convention, however that doesn’t apply to models.
Models
Do
class Student {
...
}
Don't
class StudentModel {
}
Services
In a singular fashion, for any class that contains business logic.
Do
class StudentService {
....
}
Don't
class StudentsService{
...
}
Also, Don't
class StudentBusinessLogic {
...
}
Also, Don't
class StudentBL {
...
}
Brokers
In a singular fashion, for any class that is a shim between your services and external resources.
Do
class StudentBroker {
....
}
Don't
class StudentsBroker {
...
}
Controllers
In a plural fashion, to reflect endpoints such as /api/students to expose your logic via RESTful operations.
Do
class StudentsController {
....
}
Don't
class StudentController {
...
}
Fields
A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
Naming
Class fields are named in a camel cased fashion.
Do
class StudentsController {
private readonly string studentName;
}
Don't
class StudentsController {
private readonly string StudentName;
}
Also, Don't
class StudentsController {
private readonly string _studentName;
}
Should follow the same rules for naming as mentioned in the Variables sections.
Referencing
When referencing a class private field, use this keyword to distinguish private class member from a scoped method or constructor level variable.
Do
class StudentsController {
private readonly string studentName;
public StudentsController(string studentName) {
this.studentName = studentName;
}
}
Don't
class StudentsController {
private readonly string _studentName;
public StudentsController(string studentName) {
_studentName = studentName;
}
}
Instantiations
Input Params Aliases
If the input variables names match to input aliases, then use them, otherwise you must use the aliases, especially with values passed in.
Do
int score = 150;
string name = "Josh";
var student = new Student(name, score);
Also, Do
var student = new Student(name: "Josh", score: 150);
But, Don't
var student = new Student("Josh", 150);
Also, Don't
Student student = new (...);
Honoring Property Order
When instantiating a class instance - make sure that your property assignment matches the properties order in the class declarations.
Do
public class Student
{
public Guid Id {get; set;}
public string Name {get; set;}
}
var student = new Student
{
Id = Guid.NewGuid(),
Name = "Elbek"
};
Also, Do
public class Student
{
private readonly Guid id;
private readonly string name;
public Student(Guid id, string name)
{
this.id = id;
this.name = name;
}
}
var student = new Student (id: Guid.NewGuid(), name: "Elbek");
Don't
public class Student
{
public Guid Id {get; set;}
public string Name {get; set;}
}
var student = new Student
{
Name = "Elbek",
Id = Guid.NewGuid()
};
Also, Don't
public class Student
{
private readonly Guid id;
private readonly string name;
public Student(string name, Guid id)
{
this.id = id;
this.name = name;
}
}
var student = new Student (id: Guid.NewGuid(), name: "Elbek");
Also, Don't
public class Student
{
private readonly Guid id;
private readonly string name;
public Student(Guid id, string name)
{
this.id = id;
this.name = name;
}
}
var student = new Student (name: "Elbek", id: Guid.NewGuid()); This chapter lives on GitHub, where it is written in the open. Read the source or suggest a change.