$npx -y skills add wshaddix/dotnet-skills --skill csharp-coding-standardsWrite modern, high-performance C# code using records, pattern matching, value objects, async/await, Span<T>/Memory<T>, and best-practice API design patterns. Emphasizes functional-style programming with C# 12+ features. Use when writing new C# code or refactoring existing code, d
| 1 | # Modern C# Coding Standards |
| 2 | |
| 3 | ## When to Use This Skill |
| 4 | |
| 5 | Use this skill when: |
| 6 | - Writing new C# code or refactoring existing code |
| 7 | - Designing public APIs for libraries or services |
| 8 | - Optimizing performance-critical code paths |
| 9 | - Implementing domain models with strong typing |
| 10 | - Building async/await-heavy applications |
| 11 | - Working with binary data, buffers, or high-throughput scenarios |
| 12 | |
| 13 | ## Core Principles |
| 14 | |
| 15 | 1. **Immutability by Default** - Use `record` types and `init`-only properties |
| 16 | 2. **Type Safety** - Leverage nullable reference types and value objects |
| 17 | 3. **Modern Pattern Matching** - Use `switch` expressions and patterns extensively |
| 18 | 4. **Async Everywhere** - Prefer async APIs with proper cancellation support |
| 19 | 5. **Zero-Allocation Patterns** - Use `Span<T>` and `Memory<T>` for performance-critical code |
| 20 | 6. **API Design** - Accept abstractions, return appropriately specific types |
| 21 | 7. **Composition Over Inheritance** - Avoid abstract base classes, prefer composition |
| 22 | 8. **Value Objects as Structs** - Use `readonly record struct` for value objects |
| 23 | |
| 24 | --- |
| 25 | |
| 26 | ## Naming Conventions |
| 27 | |
| 28 | ### General Rules |
| 29 | |
| 30 | | Element | Convention | Example | |
| 31 | |---------|-----------|---------| |
| 32 | | Namespaces | PascalCase, dot-separated | `MyCompany.MyProduct.Core` | |
| 33 | | Classes, Records, Structs | PascalCase | `OrderService`, `OrderSummary` | |
| 34 | | Interfaces | `I` + PascalCase | `IOrderRepository` | |
| 35 | | Methods | PascalCase | `GetOrderAsync` | |
| 36 | | Properties | PascalCase | `OrderDate` | |
| 37 | | Events | PascalCase | `OrderCompleted` | |
| 38 | | Public constants | PascalCase | `MaxRetryCount` | |
| 39 | | Private fields | `_camelCase` | `_orderRepository` | |
| 40 | | Parameters, locals | camelCase | `orderId`, `totalAmount` | |
| 41 | | Type parameters | `T` or `T` + PascalCase | `T`, `TKey`, `TValue` | |
| 42 | | Enum members | PascalCase | `OrderStatus.Pending` | |
| 43 | |
| 44 | ### Async Method Naming |
| 45 | |
| 46 | Suffix async methods with `Async`: |
| 47 | |
| 48 | ```csharp |
| 49 | public Task<Order> GetOrderAsync(int id); |
| 50 | public ValueTask SaveChangesAsync(CancellationToken ct); |
| 51 | |
| 52 | Exception: Event handlers and interface implementations where the framework does not use the `Async` suffix (e.g., ASP.NET Core middleware `InvokeAsync` is already named by the framework). |
| 53 | ``` |
| 54 | |
| 55 | ### Boolean Naming |
| 56 | |
| 57 | Prefix booleans with `is`, `has`, `can`, `should`, or similar: |
| 58 | |
| 59 | ```csharp |
| 60 | public bool IsActive { get; set; } |
| 61 | public bool HasOrders { get; } |
| 62 | public bool CanDelete(Order order); |
| 63 | ``` |
| 64 | |
| 65 | ### Collection Naming |
| 66 | |
| 67 | Use plural nouns for collections: |
| 68 | |
| 69 | ```csharp |
| 70 | public IReadOnlyList<Order> Orders { get; } |
| 71 | public Dictionary<string, int> CountsByName { get; } |
| 72 | ``` |
| 73 | |
| 74 | --- |
| 75 | |
| 76 | ## File Organization |
| 77 | |
| 78 | ### One Type Per File |
| 79 | |
| 80 | Each top-level type (class, record, struct, interface, enum) should be in its own file, named exactly as the type. Nested types stay in the containing type's file. |
| 81 | |
| 82 | ``` |
| 83 | OrderService.cs -> public class OrderService |
| 84 | IOrderRepository.cs -> public interface IOrderRepository |
| 85 | OrderStatus.cs -> public enum OrderStatus |
| 86 | OrderSummary.cs -> public record OrderSummary |
| 87 | ``` |
| 88 | |
| 89 | ### File-Scoped Namespaces |
| 90 | |
| 91 | Always use file-scoped namespaces (C# 10+): |
| 92 | |
| 93 | ```csharp |
| 94 | namespace MyApp.Services; |
| 95 | |
| 96 | public class OrderService { } |
| 97 | ``` |
| 98 | |
| 99 | ### Using Directives |
| 100 | |
| 101 | Place `using` directives at the top of the file, outside the namespace. With `<ImplicitUsings>enable</ImplicitUsings>` (default in modern .NET), common namespaces are already imported. |
| 102 | |
| 103 | Order of `using` directives: |
| 104 | 1. `System.*` namespaces |
| 105 | 2. Third-party namespaces |
| 106 | 3. Project namespaces |
| 107 | |
| 108 | --- |
| 109 | |
| 110 | ## Code Style |
| 111 | |
| 112 | ### Braces |
| 113 | |
| 114 | Always use braces for control flow, even for single-line bodies: |
| 115 | |
| 116 | ```csharp |
| 117 | if (order.IsValid) |
| 118 | { |
| 119 | Process(order); |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ### Expression-Bodied Members |
| 124 | |
| 125 | Use expression bodies for single-expression members: |
| 126 | |
| 127 | ```csharp |
| 128 | public string FullName => $"{FirstName} {LastName}"; |
| 129 | public override string ToString() => $"Order #{Id}"; |
| 130 | ``` |
| 131 | |
| 132 | ### `var` Usage |
| 133 | |
| 134 | Use `var` when the type is obvious from the right-hand side: |
| 135 | |
| 136 | ```csharp |
| 137 | var orders = new List<Order>(); |
| 138 | var customer = GetCustomerById(id); |
| 139 | |
| 140 | IOrderRepository repo = serviceProvider.GetRequiredService<IOrderRepository>(); |
| 141 | decimal total = CalculateTotal(items); |
| 142 | ``` |
| 143 | |
| 144 | ### Null Handling |
| 145 | |
| 146 | Prefer pattern matching over null checks: |
| 147 | |
| 148 | ```csharp |
| 149 | if (order is not null) { } |
| 150 | if (order is { Status: OrderStatus.Active }) { } |
| 151 | |
| 152 | var name = customer?.Name ?? "Unknown"; |
| 153 | var orders = customer?.Orders ?? []; |
| 154 | items ??= []; |
| 155 | ``` |
| 156 | |
| 157 | ### String Handling |
| 158 | |
| 159 | Prefer string interpolation over concatenation or `string.Format`: |
| 160 | |
| 161 | ```csharp |
| 162 | var message = $"Order {orderId} totals {total:C2}"; |
| 163 | |
| 164 | var json = $$""" |
| 165 | { |
| 166 | "id": {{orderId}}, |
| 167 | "n |