$npx -y skills add Aaronontheweb/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.
| 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 | ## Reference Files |
| 14 | |
| 15 | - [value-objects-and-patterns.md](value-objects-and-patterns.md): Full value object examples and pattern matching code |
| 16 | - [performance-and-api-design.md](performance-and-api-design.md): Span<T>/Memory<T> examples and API design principles |
| 17 | - [composition-and-error-handling.md](composition-and-error-handling.md): Composition over inheritance, Result type, testing patterns |
| 18 | - [anti-patterns-and-reflection.md](anti-patterns-and-reflection.md): Reflection avoidance and common anti-patterns |
| 19 | |
| 20 | ## Core Principles |
| 21 | |
| 22 | 1. **Immutability by Default** - Use `record` types and `init`-only properties |
| 23 | 2. **Type Safety** - Leverage nullable reference types and value objects |
| 24 | 3. **Modern Pattern Matching** - Use `switch` expressions and patterns extensively |
| 25 | 4. **Async Everywhere** - Prefer async APIs with proper cancellation support |
| 26 | 5. **Zero-Allocation Patterns** - Use `Span<T>` and `Memory<T>` for performance-critical code |
| 27 | 6. **API Design** - Accept abstractions, return appropriately specific types |
| 28 | 7. **Composition Over Inheritance** - Avoid abstract base classes, prefer composition |
| 29 | 8. **Value Objects as Structs** - Use `readonly record struct` for value objects |
| 30 | |
| 31 | --- |
| 32 | |
| 33 | ## Language Patterns |
| 34 | |
| 35 | ### Records for Immutable Data (C# 9+) |
| 36 | |
| 37 | Use `record` types for DTOs, messages, events, and domain entities. |
| 38 | |
| 39 | ```csharp |
| 40 | // Simple immutable DTO |
| 41 | public record CustomerDto(string Id, string Name, string Email); |
| 42 | |
| 43 | // Record with validation in constructor |
| 44 | public record EmailAddress |
| 45 | { |
| 46 | public string Value { get; init; } |
| 47 | |
| 48 | public EmailAddress(string value) |
| 49 | { |
| 50 | if (string.IsNullOrWhiteSpace(value) || !value.Contains('@')) |
| 51 | throw new ArgumentException("Invalid email address", nameof(value)); |
| 52 | |
| 53 | Value = value; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Records with collections - use IReadOnlyList |
| 58 | public record ShoppingCart( |
| 59 | string CartId, |
| 60 | string CustomerId, |
| 61 | IReadOnlyList<CartItem> Items |
| 62 | ) |
| 63 | { |
| 64 | public decimal Total => Items.Sum(item => item.Price * item.Quantity); |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | **When to use `record class` vs `record struct`:** |
| 69 | - `record class` (default): Reference types, use for entities, aggregates, DTOs with multiple properties |
| 70 | - `record struct`: Value types, use for value objects (see next section) |
| 71 | |
| 72 | ### Value Objects as readonly record struct |
| 73 | |
| 74 | Value objects should **always be `readonly record struct`** for performance and value semantics. Use explicit conversions, never implicit operators. |
| 75 | |
| 76 | ```csharp |
| 77 | public readonly record struct OrderId(string Value) |
| 78 | { |
| 79 | public OrderId(string value) : this( |
| 80 | !string.IsNullOrWhiteSpace(value) |
| 81 | ? value |
| 82 | : throw new ArgumentException("OrderId cannot be empty", nameof(value))) |
| 83 | { } |
| 84 | public override string ToString() => Value; |
| 85 | } |
| 86 | |
| 87 | public readonly record struct Money(decimal Amount, string Currency); |
| 88 | public readonly record struct CustomerId(Guid Value) |
| 89 | { |
| 90 | public static CustomerId New() => new(Guid.NewGuid()); |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | See [value-objects-and-patterns.md](value-objects-and-patterns.md) for complete examples including multi-value objects, factory patterns, and the no-implicit-conversion rule. |
| 95 | |
| 96 | ### Pattern Matching (C# 8-12) |
| 97 | |
| 98 | Use switch expressions, property patterns, relational patterns, and list patterns for cleaner code. |
| 99 | |
| 100 | ```csharp |
| 101 | public decimal CalculateDiscount(Order order) => order switch |
| 102 | { |
| 103 | { Total: > 1000m } => order.Total * 0.15m, |
| 104 | { Total: > 500m } => order.Total * 0.10m, |
| 105 | { Total: > 100m } => order.Total * 0.05m, |
| 106 | _ => 0m |
| 107 | }; |
| 108 | ``` |
| 109 | |
| 110 | See [value-objects-and-patterns.md](value-objects-and-patterns.md) for full pattern matching examples. |
| 111 | |
| 112 | --- |
| 113 | |
| 114 | ### Nullable Reference Types (C# 8+) |
| 115 | |
| 116 | Enable nullable reference types in your project and handle nulls explicitly. |
| 117 | |
| 118 | ```csharp |
| 119 | // In .csproj |
| 120 | <PropertyGroup> |
| 121 | <Nullable>enable</Nullable> |
| 122 | </PropertyGroup> |
| 123 | |
| 124 | // Explicit nullability |
| 125 | public string? FindUserName(string userId) |
| 126 | { |
| 127 | var user = _repository.Find(userId); |
| 128 | return user?.Name; |
| 129 | } |
| 130 | |
| 131 | // Pattern matching with null checks |
| 132 | public decimal GetDiscount(Customer? customer) => customer switch |
| 133 | { |
| 134 | null => 0m, |
| 135 | { IsVip: true } => 0.20m, |
| 136 | { OrderCount: > 10 } => 0.10m, |
| 137 | _ => 0.05m |
| 138 | }; |
| 139 | |
| 140 | // Guard clauses with ArgumentNullException.ThrowIfNull (C# 11+) |
| 141 | public void ProcessOrder(Order? order) |
| 142 | { |
| 143 | ArgumentNullException.ThrowIfNull(order); |
| 144 | // order is now non-nullable in this scope |
| 145 | Con |