General Coding conventions for C#
Last updated
Last updated
Coding conventions serve the following purposes:
They create a consistent look to the code, so that readers can focus on content, not layout.
They enable readers to understand the code more quickly by making assumptions based on previous experience.
They facilitate copying, changing, and maintaining the code.
They demonstrate C# best practices.
In short examples that do not include , use namespace qualifications. If you know that a namespace is imported by default in a project, you do not have to fully qualify the names from that namespace. Qualified names can be broken after a dot (.) if they are too long for a single line.
You do not have to change the names of objects that were created by using the Visual Studio designer tools to make them fit other guidelines.
Layout Conventions
Good layout uses formatting to emphasize the structure of your code and to make the code easier to read. Microsoft examples and samples conform to the following conventions:
Use the default Code Editor settings (smart indenting, four-character indents, tabs saved as spaces). For more information, see .
Write only one statement per line.
Write only one declaration per line.
If continuation lines are not indented automatically, indent them one tab stop (four spaces).
Add at least one blank line between method definitions and property definitions.
Use parentheses to make clauses in an expression apparent, as shown in the following code.
Commenting Conventions
Place the comment on a separate line, not at the end of a line of code.
Begin comment text with an uppercase letter.
End comment text with a period.
Insert one space between the comment delimiter (//) and the comment text, as shown in the following example.
Do not create formatted blocks of asterisks around comments.
Language Guidelines
The following sections describe practices that the C# team follows to prepare code examples and samples.
String Data Type
Implicitly Typed Local Variables
Do not rely on the variable name to specify the type of the variable. It might not be correct.
The following example uses implicit typing in a for
statement.
The following example uses implicit typing in a foreach
statement.
Unsigned Data Type
In general, use int
rather than unsigned types. The use of int
is common throughout C#, and it is easier to interact with other libraries when you use int
.
Arrays
Use the concise syntax when you initialize arrays on the declaration line.
Delegates
Use the concise syntax to create instances of a delegate type.
try-catch and using Statements in Exception Handling
&& and || Operators
New Operator
Use the concise form of object instantiation, with implicit typing, as shown in the following declaration.
The previous line is equivalent to the following declaration.
Use object initializers to simplify object creation.
Event Handling
If you are defining an event handler that you do not need to remove later, use a lambda expression.
Static Members
LINQ Queries
Use meaningful names for query variables. The following example uses seattleCustomers
for customers who are located in Seattle.
Use aliases to make sure that property names of anonymous types are correctly capitalized, using Pascal casing.
Rename properties when the property names in the result would be ambiguous. For example, if your query returns a customer name and a distributor ID, instead of leaving them as Name
and ID
in the result, rename them to clarify that Name
is the name of a customer, and ID
is the ID of a distributor.
Use implicit typing in the declaration of query variables and range variables.
Use to concatenate short strings, as shown in the following code.
To append strings in loops, especially when you are working with large amounts of text, use a object.
Use for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.
Do not use when the type is not apparent from the right side of the assignment.
Avoid the use of var
in place of .
Use implicit typing to determine the type of the loop variable in and loops.
Use a statement for most exception handling.
Simplify your code by using the C# . If you have a statement in which the only code in the finally
block is a call to the method, use a using
statement instead.
To avoid exceptions and increase performance by skipping unnecessary comparisons, use instead of and instead of when you perform comparisons, as shown in the following example.
Call members by using the class name: ClassName.StaticMember. This practice makes code more readable by making static access clear. Do not qualify a static member defined in a base class with the name of a derived class. While that code compiles, the code readability is misleading, and the code may break in the future if you add a static member with the same name to the derived class.
Align query clauses under the clause, as shown in the previous examples.
Use clauses before other query clauses to ensure that later query clauses operate on the reduced, filtered set of data.
Use multiple from
clauses instead of a clause to access inner collections. For example, a collection of Student
objects might each contain a collection of test scores. When the following query is executed, it returns each score that is over 90, along with the last name of the student who received the score.