Skip to content

Latest commit

 

History

History
77 lines (51 loc) · 1.78 KB

c_sharp_style_guide.md

File metadata and controls

77 lines (51 loc) · 1.78 KB

C# Style Guide and Coding Conventions

Overview

In general, follow the Microsoft C# Coding Guidelines described in the following links:

Exceptions

The following guidelines supersede the Microsoft C# Coding Guidelines, and should be used.

Type Inference

Use type inference (var) wherever possible. This can improve readability and ease of refactoring.

One-line if statements

Add braces to one-line if statements;

# Yes:
if (isEmpty)
{
    callFun();
}

# No:
if (isEmpty)
    callFun();

Rationale

Avoiding braces can cause developers to miss bugs, such as Apple's infamous goto-fail bug

Prefer Range for simple loop iteration

As an example, to loop 0, 1, 2, 3:

# Yes:
using static System.Linq.Enumerable;

foreach (var i in Range(0, 4))

# No:
for (var i = 0; i < 4; i++)

The signature of Range is:

Range (int start, int count);

Another example that loops 1, 2, 3:

# Yes:
using static System.Linq.Enumerable;

foreach (var i in Range(1, 3))

# No:
for (var i = 1; i < 4; i++)

Rationale

  • Only need to mention loop variable (e.g. i) once
  • Remove some error-prone boilerplate (i++)
  • Remove the possibly of incrementing the wrong value (e.g. incrementing i instead of j in an inner loop)
  • Express clearly the intent