§ 2 — The C# Coding Standard
Variables
Naming
Variable names should be concise and representative of nature and the quantity of the value it holds or will potentially hold.
Names
Do
var student = new Student();
Don't
var s = new Student();
Also, Don't
var stdnt = new Student();
The same rule applies to lambda expressions:
Do
students.Where(student => student ... );
Don't
students.Where(s => s ... );
Plurals
Do
var students = new List<Student>();
Don't
var studentList = new List<Student>();
Names with Types
Do
var student = new Student();
Don't
var studentModel = new Student();
Also, Don't
var studentObj = new Student();
Nulls or Defaults
If a variable value is its default such as 0 for int or null for strings and you are not planning on changing that value (for testing purposes for instance) then the name should identify that value.
Do
Student noStudent = null;
Don't
Student student = null;
Also, Do
int noChangeCount = 0;
But, Don't
int changeCount = 0;
Declarations
Declaring a variable and instantiating it should indicate the immediate type of the variable, even if the value is to be determined later.
Clear Types
If the right side type is clear, then use var to declare your variable
Do
var student = new Student();
Don't
Student student = new Student();
Semi-Clear Types
If the right side isn’t clear (but known) of the returned value type, then you must explicitly declare your variable with its type.
Do
Student student = GetStudent();
Don't
var student = GetStudent();
Unclear Types
If the right side isn’t clear and unknown (such as an anonymous types) of the returned value type, you may use var as your variable type.
Do
var student = new
{
Name = "Hassan",
Score = 100
};
Single-Property Types
Assign properties directly if you are declaring a type with one property.
Do
var inputStudentEvent = new StudentEvent();
inputStudentEvent.Student = inputProcessedStudent;
Don't
var inputStudentEvent = new StudentEvent
{
Student = inputProcessedStudent
};
Also, Do
var studentEvent = new StudentEvent
{
Student = someStudent,
Date = someDate
};
Don't
var studentEvent = new StudentEvent();
studentEvent.Student = someStudent;
studentEvent.Date = someDate;
Organization
Breakdown
If a variable declaration exceeds 120 characters, break it down starting from the equal sign.
Do
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
Don't
List<Student> washingtonSchoolsStudentsWithGrades = await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
Multiple Declarations
Declarations that occupy two lines or more should have a new line before and after them to separate them from previous and next variables declarations.
Do
Student student = GetStudent();
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
School school = await GetSchoolAsync();
Don't
Student student = GetStudent();
List<Student> washingtonSchoolsStudentsWithGrades =
await GetAllWashingtonSchoolsStudentsWithTheirGradesAsync();
School school = await GetSchoolAsync();
Also, declarations of variables that are of only one line should have no new lines between them.
Do
Student student = GetStudent();
School school = await GetSchoolAsync();
Don't
Student student = GetStudent();
School school = await GetSchoolAsync(); This chapter lives on GitHub, where it is written in the open. Read the source or suggest a change.