Coding Conventions for C# - JMcQueen1000/Battleships GitHub Wiki
Coding Conventions Description:
Coding conventions are used by companies all over the world to keep uniformity and order throughout programs. This has a number of benefits for programmers as it:
- Allows for the best coding practices.
- Keeps the code neat and aesthetically pleasing.
- Makes it easier to understand when reading for the first time.
- Makes it easier to copy and maintain the code.
Naming Conventions:
In short examples that do not include using directives, 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, as shown in the following example.
Layout Conventions:
Good layout uses formatting to emphasise 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).
- 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 brackets to make clauses in an expression apparent.
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.
Language Guidelines:
Strings:
- Use the '+' operator to concatenate 2 strings
Implicitly Typed Local Variables:
- Use implicit typing 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.
- When the variable type is not obvious on the right side, use an explicit type
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 initialise arrays on the declaration line.
Preferred syntax:
string[] vowels1 = { "a", "e", "i", "o", "u" };
If you use explicit instantiation, you can use var.
var vowels2 = new string[] { "a", "e", "i", "o", "u" };
If you specify an array size, you must initialise the elements one at a time.
var vowels3 = new string[5];
vowels3[0] = "a";
vowels3[1] = "e";
etc.
Delegates:
- Use the concise syntax to create instances of a delegate type.
&& and || Operators:
- Use && instead of & and || instead of | when you perform comparisons. This is to avoid exceptions and increase performance by skipping unnecessary comparisons.
New Operator:
- Use the concise form of object instantiation, with implicit typing.
var instance1 = new ExampleClass();
is equivalent toExampleClass instance2 = new ExampleClass();
.
Source for said coding practices: C# Coding Conventions - C#