Coding Style
General Guidelines
Naming Conventions
- Use
PascalCase for class, method, and property names.
- Use
camelCase for local variables and method parameters.
- Use
ALL_CAPS for constants.
- Prefix interfaces with
I (e.g., IOrderService).
- Use meaningful, descriptive names; avoid abbreviations.
- Use 4 spaces for indentation (no tabs).
- Use file-scoped namespaces to simplify structure and improve readability.
- Add a blank line between method definitions.
- Place opening braces on a new line for methods, properties, and types (unless using file-scoped namespaces, then follow the file-scoped style).
Example: File-Scoped Namespaces
// Before
namespace MyNamespace
{
public class ExampleClass
{
// ...existing code...
}
}
// After
namespace MyNamespace;
public class ExampleClass
{
// ...existing code...
}
- All new files must use file-scoped namespaces. Refactor existing files during updates or maintenance.
Variable Declaration
- Use
var for local variable declarations when the type is obvious.
- Prefer explicit types if it improves clarity.
Example
// Before
int x = 1;
double y = 2.0;
string z = "Hello";
ProductBacklogItem item = new ProductBacklogItem("Test", "Test", 1, 1, 1);
// After
var x = 1;
var y = 2.0;
var z = "Hello";
var item = new ProductBacklogItem("Test", "Test", 1, 1, 1);
Sealed Classes
- Make classes
sealed by default. If a class needs to be inherited, mark it as virtual explicitly.
Use Nameof with Exceptions
- When throwing exceptions, use
nameof to refer to the parameter name instead of hardcoding it.
Example
// Before
throw new ArgumentNullException("parameterName");
// After
throw new ArgumentNullException(nameof(parameterName));
Code Structure
- One type per file (class, interface, enum, etc.).
- Organize files by feature/domain when possible.
- Group using directives at the top of the file, outside the namespace.
- Place related types in the same namespace.
- Use partial classes only when necessary (e.g., for code generation).
- Use XML documentation comments (
///) for public APIs.
- Write comments to explain why, not what, when necessary.
- Remove commented-out code before committing.
Null Checks & Exceptions
- Use guard clauses for argument validation.
- Use
nameof for parameter names in exceptions.
Modern C# Features
- Use pattern matching and expression-bodied members where appropriate.
- Prefer object and collection initializers.
References