| name | csharp-coding-standards |
| description | **REVIEW/FORMAT SKILL** — Enforce official Microsoft C# coding conventions for naming, style, and language usage. WHEN: "review C# code", "check coding standards", "format C# code", "enforce C# conventions", "naming conventions", "C# style check", "code review", "check csharp standards". INVOKES: grep, view, edit. |
C# Coding Standards
Enforces Microsoft's official C# coding conventions and identifier naming rules when reviewing or formatting C# code.
User Input
$ARGUMENTS
You MUST consider the user input before proceeding (if not empty).
Modes
Review Mode (default)
When the user asks to review C# code, scan for violations and report them grouped by category. Do not modify files unless asked.
Format Mode
When the user asks to format or fix C# code, apply corrections directly using the edit tool.
Rules
1 · Naming Conventions
| Element | Casing | Prefix | Example |
|---|
| Class, struct, record, delegate, enum | PascalCase | — | DataService |
| Interface | PascalCase | I | IWorkerQueue |
| Public field, property, event, method, local function | PascalCase | — | StartProcessing() |
| Private / internal field | camelCase | _ | _workerQueue |
| Private / internal static field | camelCase | s_ | s_workerQueue |
| Thread-static field | camelCase | t_ | t_timeSpan |
| Local variable, method parameter | camelCase | — | isValid |
| Constant (field or local) | PascalCase | — | MaxRetryCount |
| Generic type parameter | PascalCase | T | TSession |
| Attribute type | PascalCase | — (suffix Attribute) | ObsoleteAttribute |
| Enum (non-flags) | Singular noun | — | Color |
| Enum (flags) | Plural noun | — | FilePermissions |
| Record primary constructor params | PascalCase | — | Person(string FirstName) |
| Class/struct primary constructor params | camelCase | — | DataService(ILogger logger) |
Additional rules:
- Prefer clarity over brevity. Use meaningful, descriptive names.
- Avoid abbreviations except widely known ones (
Id, Url, Html).
- Avoid single-letter names except simple loop counters.
- Never use two consecutive underscores (
__).
2 · Type References
- Use language keywords, not runtime types:
string not System.String, int not System.Int32.
- Prefer
int over unsigned types unless domain-required.
3 · var Usage
- ✅ Use
var when type is obvious from the right side (new, cast, literal).
- ✅ Use
var in for loop variables.
- ✅ Use
var for LINQ query results.
- ❌ Don't use
var when type isn't apparent from the expression.
- ❌ Don't use
var in foreach — use explicit element type.
- ❌ Never use
var in place of dynamic.
4 · String Handling
- Use string interpolation (
$"") for short concatenation.
- Use
StringBuilder for loops or large text assembly.
- Prefer raw string literals (
"""...""") over escape sequences / verbatim strings.
- Prefer expression-based interpolation over
String.Format.
5 · Modern Constructs (prefer when applicable)
- Collection expressions:
string[] vowels = ["a", "e", "i", "o", "u"];
Func<> / Action<> over custom delegate types.
using declarations (braceless) over try-finally for IDisposable.
- Target-typed
new(): ExampleClass instance = new();
- Object initializers over sequential property assignment.
required properties over constructor-forced initialization.
- File-scoped namespaces:
namespace MyApp;
- Lambda event handlers for non-removable subscriptions.
&& / || (short-circuit) instead of & / | for boolean logic.
6 · Exception Handling
- Catch specific exception types, never bare
System.Exception.
- Use
using statements/declarations for IDisposable, not try-finally.
- Re-throw with
throw; not throw ex;.
7 · Async
- Use
async / await for I/O-bound operations.
- Use
ConfigureAwait where appropriate.
8 · LINQ
- Use meaningful query variable names.
- Alias anonymous type properties with PascalCase.
- Align clauses under the
from keyword.
- Place
where before other clauses.
- Prefer multiple
from over join for inner collections.
9 · Formatting & Layout
- 4 spaces indentation — no tabs.
- Allman braces — opening and closing brace each on own line.
- One statement per line. One declaration per line.
- Blank line between method/property definitions.
- Use parentheses to clarify operator precedence.
- Line break before binary operators when wrapping.
using directives outside namespace declarations.
10 · Comments
- Single-line
// comments only — avoid /* */.
- XML doc comments (
///) for all public members.
- Comments on separate lines, not end-of-line.
- Begin with uppercase, end with period, space after
//.
11 · Static Members
- Call via class name:
ClassName.StaticMember.
- Never qualify base-class statics with a derived class name.
Review Output Format
When reviewing, report violations as:
## C# Coding Standards Review
### ❌ Violations Found
**Naming** (Rule 1)
- `src/Foo.cs:12` — Private field `workerQueue` missing `_` prefix → `_workerQueue`
**var Usage** (Rule 3)
- `src/Bar.cs:45` — `var result = GetCount();` — type not obvious, use explicit `int`
### ✅ Compliant
- String handling
- Exception handling
- Formatting & layout
Omit compliant categories if violations exist in every category. Always show the violation count summary at the top.