Skip to content

Instantly share code, notes, and snippets.

@alanstevens
Last active October 8, 2018 03:29
Show Gist options
  • Save alanstevens/437495036f8aaba9e20281e9131921ec to your computer and use it in GitHub Desktop.
Save alanstevens/437495036f8aaba9e20281e9131921ec to your computer and use it in GitHub Desktop.
Proposed C# Coding Standard

Proposed C# Coding Standard

Naming

  • Method, property and class names are PascalCased (as opposed to camelCase, snake_case or kebob-case)
  • Local variables and parameters are camelCased
  • Private fields start with an underscore and are _camelCase.
  • Classes, methods and variable names should have meaningful English names using whole words where it makes sense.
  • Avoid unnecessary contractions in your names. e.g. "Product" instead "Prod"
  • Give your DTOs meaningful English names. DO NOT rename any properties on a DTO. They are necessary for mapping to the stored procedure.
  • When converting FoxPro code do not automatically use the existing variable and function names.
  • Avoid Hungarian Notation (https://en.wikipedia.org/wiki/Hungarian_notation).
  • Specifically, do not prefix your names with 'g' or 'l' to indicate variable scope.
  • Do not prefix you names with 'n', 'l' or 'c' etc. to indicate the variable type.

General Guidelines

This
{
}
NotThis{
}
  • use implicitly typed local variables (var) wherever possible. (you can always hover your mouse above the var keyword if you are not sure of the type).
  • If a member is only used within the class, use a _field and not a property.
  • Avoid comments at the end of a line. Place them on the line above, instead.
  • Similarly, put attributes on the line above and not inline.
  • Use collection initializers. Resharper will give you a hint where that is possible.
  • Use class initializers. Again Resharper will recommend this
  • Create extension methods to encapsulate repetitive code. If you see an expression or statement ~five or more times, then it's probably time to pull it into an extension method. A well-named extension method will make the code less cluttered and more readable.
  • Use extension methods, not query syntax, for linq queries. e.g. MyCollection.Select(x => x.Name);
  • For single line methods, use an expression body. e.g. public void DisplayName() => Console.WriteLine(ToString());
  • The above is especially appropriate for constructors.
  • Use dollar sign string interpolation rather than String.Format(). e.g. $"Today is {date.DayOfWeek}, it's {date:HH:mm} now."
  • Use "question mark dot" notation to avoid null reference exceptions. e.g. MyThing?.OnlyEvaluatedIfMyThingIsNotNull();
  • Remove unused using directives. Unused usings will be grayed out by Resharper. Right click on one and select "Remove and Sort Usings"
  • To get the first member of a collection, use .FirstOrDefault() and not [0] or First().
  • Use optional parameters. No need to pass unnecessary default values.
  • If you have to pass ~five or more parameters, consider creating a class to use as a parameter.
  • There is no need to create a property for a private class member. Use a simple _field instead.
  • In a class, leave one blank line between methods. This does not apply to properties, fields or interfaces.
  • If your unit test does not have a return value to verify, use Assert.DoesNotThrow and not Assert.That(true).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment