Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

C#

A mostly reasonable approach to C#.

  1. Basic Rules
  2. Formatting
  3. Naming

TIP: Use the .editorconfig file to maintain consistency

Basic Rules

Prefer var keyword

Use var to avoid redundant reputation of type names and simplify generic code.

DO:

var age = GetAge();

DON'T:

int age = GetAge();

Avoid comments

Use comments sparingly and only when there is unusual behaviour that needs explanation; focus on the why and what of a code block and not the how.

Avoid line-by-line commentary; code should be self-documenting and easy for other developers to understand. Comments simply get in the way of reading code, add an unnecessary overhead during refactoring, and very easily fall out-of-date.

Formatting

Structure

internal class Worker
{
    private readonly IDependency _dependency;
    private int _counter;

    public bool SomeProperty { get; init; }

    public Worker(IDependency dependency)
    {
        _dependency = dependency;
        _counter = 0;
    }

    public void DoSomething()
    {
        _counter++;
        DoSomethingElse();
    }

    private void DoSomethingElse()
    {
        _dependency.SomethingElse();
        Log.Information("Counter is set to {counter}", _counter);
    }
}
  1. Private fields
  2. Public properties
  3. Constructors
  4. Public methods
  5. Private methods

Prefer spaces for indentation

Prefer an indent size of 4

Prefer one class per file

For simplicity and code navigation only define one class per file.

An exception to this is the expansion into generics.

DO:

// Result.cs
namespace Helpers
{
    internal class Result
    { }

    internal class Result<TData> : Result
    { }
}

DON'T:

// Result.cs
namespace Helpers
{
    internal class Result
    { }

    internal class Result<TData> : Result
    { }

    internal class ResultError
    { }

    internal class Retry
    { }
}

Avoid unused using directives

Remove any unused using statements from the top of each file.

Prefer using directives to be placed oustide of the namespace

DO:

using System;

namespace Foo
{
    internal class Bar
    { }
}

DON'T:

namespace Foo
{
    using System;

    internal class Bar
    { }
}

Prefer to sort using directives first by system then alphabetically

using System;
using System.Threading.Tasks;
using Bar;
using Foo;
using Foo.Baz;

Naming

Casing

File - Pascal case

MyFile.cs

Class - Pascal case

public class MyClass {...}

Method - Pascal case

public class Foo
{
  public void MyMethod() {...}
}

Property - Pascal case

public class Foo
{
  public int MyProperty { get; set; }
}

Field - Lower camel case with leading underscore

public class Foo
{
  private int _myField;
}

Parameter - Lower camel case

public void Foo(int myParameter) {...}

Variable - Lower camel case

public void Foo()
{
  var myVariable = 413;
}

Constants - Screaming snake case

public class Foo
{
  private const string MY_CONSTANT = "*screams*";
}

Avoid Hungarian Notation

The compiler and IDE can provide type information, prefixing this information is redundant and gets in the way of reading the code.

DO:

var username = "batman";

DON'T:

var strUsername = "batman";

Prefer to match class name and file name

For simplicity and code navigation keep the class name and the file name the same.

Prefer to match namespace to the folder structure

For simplicity and code navigation keep the namespace declaration consistent to the folder structure.