C# — Style Guide Cheat Sheet


Writing beautiful, understandable code is just as important as writing a production code. There are many different guides and guidelines on the Internet.


I tried to go through all of them and put together a little cheat sheet. I have included only those rules that affect the style of the code and cover most of the key points.


Hope you find this very useful!


Layout

  • A maximum of one statement per line.

  • A maximum of one assignment per statement.

  • Indentation of 4 spaces, no tabs.

  • Column limit: 120.

  • Line break before opening brace.

  • Line break between closing brace and else.

  • Space after if/for/while etc., and after commas.

  • No space after an opening parenthesis or before a closing parenthesis.

  • One space between the operator and each operand of all other operators.

  • Add at least one blank line between method definitions and property definitions.


Comments

  • Place the comment on a separate line, not at the end of a line of code.

  • Insert one space between the comment delimiter (//) and the comment text.


Naming

Do:

  • Names of classes, methods, enumerations, public fields, public properties, namespaces: PascalCase.

  • Names of local variables, parameters: camelCase.

  • Names of private, protected, internal and protected internal fields and properties: _camelCase.

  • Naming convention is unaffected by modifiers such as const, static, readonly, etc.