Repository navigation
C# Coding Standards
Prefix every instance and static field name with a lowercase f followed by PascalCase, regardless of its access modifier. Examples include fName, fPostalCode, and fUserCount.
| Element | Rule | Examples |
|---|---|---|
| Classes | PascalCase, without technical class prefixes or suffixes |
UserManager. Avoid CUserManager and UserManagerClass. |
| Enums |
E followed by PascalCase |
EType |
| Constants | UPPER_SNAKE_CASE | MAX_RETRY_COUNT |
| Ordinary custom methods and functions | PascalCase |
LoadUsers, DeleteUser
|
| Properties | PascalCase |
Name, PageSize
|
| Local variables and parameters | camelCase, without the field prefix |
productCount, userID, isEnabled
|
Use descriptive names. Reserve the f prefix for class fields. Constants follow their own UPPER_SNAKE_CASE rule.
- Always declare the type of each variable explicitly, including loop variables and variables in documentation examples. Do not use
var. - Always write the type name after
newin constructor calls, even when the compiler can infer it. For example, writenew object()ornew List<string>(). Avoid the target-typed formsnew()andnew(...).
Explicit declarations
int productCount = 0;
string productName = "Camera";
List<string> productNames = new List<string>();Implicit declarations to avoid
var productCount = 0;
var productName = "Camera";
var productNames = new List<string>();Use Allman (BSD) brace style. Put the opening brace { on the next line after the declaration or statement and the closing brace } on its own line. Align both braces with the start of the declaration or statement.
This applies to classes, interfaces, structs, enums, constructors, methods, functions, conditionals, loops, switch statements, try/catch/finally blocks, and anonymous blocks.
Keep method calls, constructor calls, assignments, return statements, and conditions on one line when practical. Do not put each argument on a separate line solely for formatting.
Line breaks are appropriate when a line becomes difficult to read, complex logic needs them, or the surrounding code already uses a multiline fluent API or builder style. Do not reformat unrelated sections.
Always use using (...) with a braced block for disposable resources. Each nested using statement must have its own braced block. This rule applies to new and modified C# code in the library, applications, tests, demos, and documentation examples.
Put the opening and closing braces on separate lines, following Allman style, and declare the resource with an explicit type. Keep all operations that need the resource inside its block.
For example, with the connection settings, languages, and Product contract already defined:
using (BlueprintDatabase db = new SqlDatabase("MyEshopDatabase", sqlServerConnectionString, languages))
{
db.RegisterEntities(typeof(Product));
}Do not write using BlueprintDatabase db = new SqlDatabase(...);. For asynchronous disposal, use await using (...) with an explicit block as well, instead of an await using declaration.
This rule concerns resource disposal. Namespace imports such as using Blueprint.Database; remain unchanged. When converting existing code, preserve resource lifetime and disposal order without reformatting unrelated code.
Use the following order for class members.
- Constants and fields.
- Constructors.
- Other methods.
- Properties.
The following example demonstrates member order, naming, explicit types, Allman braces, and English documentation comments:
using System;
/// <summary>Stores a product name with a maximum supported length.</summary>
public class ProductSettings
{
private const int MAX_NAME_LENGTH = 200;
private string fName;
/// <summary>Initializes the settings with a validated product name.</summary>
/// <param name="name">The initial product name.</param>
/// <exception cref="ArgumentNullException">The name is null.</exception>
/// <exception cref="ArgumentException">The name exceeds the maximum length.</exception>
public ProductSettings(string name)
{
fName = string.Empty;
Name = name;
}
/// <summary>Clears the stored product name.</summary>
public void ResetName()
{
fName = string.Empty;
}
/// <summary>Gets or sets the product name.</summary>
/// <exception cref="ArgumentNullException">The assigned value is null.</exception>
/// <exception cref="ArgumentException">The assigned value exceeds the maximum length.</exception>
public string Name
{
get
{
return fName;
}
set
{
ArgumentNullException.ThrowIfNull(value);
if (value.Length > MAX_NAME_LENGTH)
{
throw new ArgumentException("The product name is too long.", nameof(value));
}
fName = value;
}
}
}Use properties to read or assign a value. Include a get accessor for reading, a set accessor for assigning, or both. Do not define separate getter or setter methods for property access.
Write properties with Allman-style braces. Give each implemented get and set accessor its own braced block. Do not use expression bodies (=>) for properties or accessors.
The Name property below reads and writes the fName field.
/// <summary>Gets or changes the stored name.</summary>
public string Name
{
get
{
return fName;
}
set
{
fName = value;
}
}Use get; and set; for declarations of abstract or interface accessors without implementations. Preserve existing accessor visibility and behavior when changing its formatting.
LINQ lambda expressions may use =>, as in item => item.Price > 0m.
Place an XML documentation comment (///) immediately above the declaration of every new method, function, and constructor, even for small members. Describe the member's purpose.
Include the applicable XML tags.
-
<summary>for the purpose. -
<param>for each parameter. -
<returns>when a value is returned. -
<exception>for relevant exceptions the caller may receive.
When substantially modifying an existing method or function that lacks documentation, add it. Keep existing documentation consistent with behavior. Use the equivalent documentation format for other languages when applicable.
Inline comments should explain business rules, workarounds, edge cases, technical constraints, or the reason for a decision. Avoid comments that merely restate obvious code. All documentation and inline comments must be in English.
Use == null and != null for simple null checks. Do not use is null, is not null, or is { } for these checks.