- 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.
- Indent by four spaces. No tabs. Install Fix Mixed Tabs (https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.FixMixedTabs) This will alert you if there are tabs and allow you to easily convert them to spaces
- Go to Tools -> Options -> Text Editor -> C# -> Tabs: Tab Size:4, Indent Size:4, Insert spaces
- Place the curly brace below the class or method name:
This
{
}
NotThis{
}
- use implicitly typed local variables (
var
) wherever possible. (you can always hover your mouse above thevar
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]
orFirst()
. - 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 notAssert.That(true)
.