The style is similar to dotnet/corefx code style except for field prefixes. The general rule to follow is "use Visual Studio defaults".
- Use Allman style braces, where each brace begins on a new line. A single line statement block can go without braces but the block must be properly indented on its own line and must not be nested in other statement blocks that use braces. One exception is that a
using
statement is permitted to be nested within anotherusing
statement by starting on the following line at the same indentation level, even if the nestedusing
contains a controlled block. - Use four spaces of indentation (no tabs).
- Use
camelCase
for internal and private fields and usereadonly
where possible. Do not prefix any fields. When used on static fields,readonly
should come afterstatic
(e.g.static readonly
notreadonly static
). Public fields should be used sparingly and should usePascalCase
with no prefix when used. - Use
PascalCase
to name all constant local variables and fields. - Public (auto) properties are preferred over public fields except in simple data structs. Use
PascalCase
for properties. - Use
IPascalCase
for interfaces andTPascalCase
for type parameters. UsePascalCase
for all other types and members. - Avoid
this.
unless absolutely necessary. - Always specify the visibility, even if it's the default (e.g.
private string foo
notstring foo
). Visibility should be the first modifier (e.g.public abstract
notabstract public
). - Namespace imports should be specified at the top of the file, outside of
namespace
declarations, and should be sorted alphabetically, with the exception ofSystem.*
namespaces, which are to be placed on top of all others. - Avoid more than one empty line at any time. For example, do not have two blank lines between members of a type.
- Avoid spurious free spaces.
For example avoid
if (someVar == 0)...
, where the dots mark the spurious free spaces. Consider enabling "View White Space (Ctrl+E, S)" if using Visual Studio to aid detection. - Only use
var
when it's obvious what the variable type is (e.g.var stream = new FileStream(...)
notvar stream = OpenStandardInput()
). - Use language keywords instead of BCL types (e.g.
int, string, float
instead ofInt32, String, Single
, etc) for both type references as well as method calls (e.g.int.Parse
instead ofInt32.Parse
). - Use
nameof(...)
instead of"..."
whenever possible and relevant. - Keep similar type members together. General file layout is:
- Public delegates
- Public enums
- Static fields and constants
- Fields
- Constructors
- Properties, indexers
- Interface implementations
- All other members
- Nested types
- When including non-ASCII characters in the source code use Unicode escape sequences (\uXXXX) instead of literal characters. Literal non-ASCII characters occasionally get garbled by a tool or editor.
- When using labels (for
goto
), indent the label one less than the current indentation. - Keep lines of code reasonably long (ReSharper is set to wrap at 120 characters), wrap longer lines. This way the code fits inside the editor window and it makes inspecting diffs easier.
An EditorConfig file (.editorconfig
) has been provided at the root of the repository, enabling C# auto-formatting conforming to the above guidelines.