Csharp modern idioms

From FerroxLabs/murage · View interactive version

Csharp modern idioms is an AI agent skill from the GitHub repository FerroxLabs/murage. Teaches expert-level modern C# patterns: records, pattern matching, nullable reference types, primary constructors, file-scoped namespaces, and global usings for C# 12+. Use when the user asks about C# records, pattern matching, nullable reference types, primary constructors, C# 12, modern C#. Do NO

How to use this skill

Skill file

Csharp Modern Idioms

When to Use

Use this skill when:

Do NOT use this skill when:


Process

1. Identify the C# Version and Enable Modern Features

Before applying any idiom, confirm what language version is active and what features are available.


2. Choose the Right Type Declaration for Data

Apply a consistent decision framework for every new data-holding type:


3. Apply Pattern Matching Systematically

Pattern matching in C# 8--12 covers 11+ pattern forms. Apply them in this priority order:


4. Migrate Nullable Reference Types Incrementally

Enabling <Nullable>enable</Nullable> on a large existing codebase without a plan causes hundreds of warnings simultaneously. Follow this phased approach:


5. Use Primary Constructors Correctly (C# 12)

Primary constructors change where and how constructor parameters live. The rules are subtle:


6. Apply File-Scoped Namespaces and Global Usings

These features reduce indentation and boilerplate with zero semantic change:


7. Compose and Validate with `required` Members and `init` Setters

Enforce object completeness without verbose constructor overloads:


8. Enforce and Review Through Tooling

Modern C# idioms drift without automated enforcement:


Output Format

When helping a user modernize or write C# code, structure your response as follows:

## Diagnosis

What C# version / .NET TFM is active:
What the current code pattern is and what problem it causes:
Which modern idiom(s) address the problem:

## Modernized Code

### Before

// original code with annotation of what is problematic


### After

// modernized code with inline comments explaining the idiom


## Key Idioms Applied

| Idiom | C# Version | Benefit | Trade-off |
|-------|-----------|---------|-----------|
| record class | 9+ | Value equality, with-expressions, ToString | Reference type; cannot inherit from non-record class |
| switch expression | 8+ | Exhaustiveness checking, no fall-through | Can become dense beyond 8-10 arms |
| property pattern | 8+ | Flattens nested null/property checks | Requires familiarity to read at speed |
| nullable reference types | 8+ | Compile-time null safety | Migration cost on legacy codebases |
| primary constructors | 12 | Reduces DI boilerplate | Parameters are not auto-properties |
| required + init | 11 + 9 | Enforces completeness without overloads | Does not work with some ORMs that need setters |
| file-scoped namespace | 10+ | Removes indentation level | File can only have one namespace |
| list patterns | 11+ | Concise array/span matching | Only works on indexable types |

## Migration Guidance (if applicable)

Step-by-step migration plan with file ordering and nullable phase:
1. [specific action]
2. [specific action]

## Tooling Configuration

Relevant .editorconfig or .csproj settings to enforce the applied idioms:

---

## Rules

1. **Never recommend `record` for EF Core entities.** EF Core's change tracker relies on reference equality and mutable properties. Records override `Equals`/`GetHashCode` by value, causing the tracker to misidentify entities. Use `class` with `required init` properties for DTO projection targets, not the entity itself.

2. **Never use the null-forgiving operator (`!`) more than once per method.** Each `!` is a contract violation with the compiler. More than one per method indicates the nullable model was not properly designed. Surface the real cause: a missing null check, a wrong API return type annotation, or a `required` member that should prevent null.

3. **Always prefer `is null` over `== null` for nullable reference type checks.** `== null` can be overloaded; `is null` always means reference equality to null and is pattern-matching-consistent. Consistency also enables the compiler to suggest further pattern rewrites.

4. **Never apply `record struct` to types larger than 16 bytes without benchmarking.** `record struct` is copied by value on every assignment and method argument. A struct with 5+ fields frequently outweighs its allocation savings. Measure with BenchmarkDotNet before committing to the shape.

5. **Never globalize a namespace used in fewer than 5 files.** Global usings are project-wide implicit imports. Globalizing rarely-used namespaces hides intent and causes confusion about where types come from. The 5-file threshold is a practical heuristic.

6. **Always seal records that are not meant to be inherited.** Records support inheritance, but unsealed records cause structural equality to include an `EqualityContract` property check that breaks equality between base and derived instances. Use `sealed record` unless the hierarchy is intentional and equality semantics across levels are designed explicitly.

7. **Never use positional record syntax when order is not meaningful.** `record Person(string FirstName, string LastName)` creates a constructor where argument order is the only differentiator for two `string` parameters. If the type has more than 2 parameters of the same type, use named `required init` properties instead to prevent argument transposition bugs.

8. **Always exhaustively handle switch expressions on discriminated unions.** When switching over a sealed hierarchy or enum, the compiler's exhaustiveness checker only works if all cases are covered. Add a discard arm (`_ => throw new UnreachableException()`) rather than silently returning a default -- the exception surfaces logic errors immediately.

9. **Never mix mutable state into a `record class` unless the mutable properties are explicitly excluded from equality.** Any mutable property participates in value equality by default. A record with a mutable collection property (`List<T>`) will produce incorrect equality results because `List<T>` uses reference equality. Override `PrintMembers` or use a custom `EqualityContract` to handle these cases, or avoid mutable collections in records entirely.

10. **Always use `ArgumentNullException.ThrowIfNull()` (.NET 6+) at method entry points instead of manual null checks.** It is shorter, throw-annotated for nullable flow analysis, and consistent. Replace `if (param == null) throw new ArgumentNullException(nameof(param));` everywhere. For range checks, use `ArgumentOutOfRangeException.ThrowIfNegative()` and related overloads (.NET 8).

---

## Edge Cases

### Legacy Codebase with Nullable Disabled Project-Wide

When `<Nullable>disable</Nullable>` or no nullable setting is present and the codebase is large (100+ files), do not enable it project-wide immediately.

- Add `<Nullable>enable</Nullable>` only to the `.csproj` of a new class library or feature area that is being extracted
- For files within the existing project, use `#nullable enable` at the top of only the files you are actively modifying in the current sprint
- Audit total nullable warnings with `dotnet build 2>&1 | grep "warning CS8"` before enabling project-wide -- if the count exceeds 200, treat each 20-warning batch as a separate story
- Never silence warnings with `#pragma warning disable CS8600` as a blanket fix -- it defeats the purpose and accumulates hidden debt

### Records and JSON Serialization (System.Text.Json)

Records with positional constructors serialize correctly with `System.Text.Json` in .NET 6+ because the deserializer supports parameterized constructors. However, edge cases exist:

- If a record has both a positional constructor and a custom additional constructor, `System.Text.Json` may pick the wrong one -- annotate the intended constructor with `[JsonConstructor]`
- `EqualityContract` (an internal property used by records) is not serialized, but if you use `JsonSerializer.SerializeToNode` and then deserialize into a base type, the derived type information is lost -- use `[JsonDerivedType]` attribute (introduced in .NET 7) for polymorphic serialization
- `record struct` with `System.Text.Json` requires the struct to have a parameterless constructor (auto-generated by the compiler) -- this works by default but breaks if you add a `[JsonConstructor]` annotation on the positional constructor on older SDK versions

### Pattern Matching Over External / Third-Party Types

When you do not control the type hierarchy (e.g., matching against `HttpStatusCode`, `Exception` subclasses, or cloud SDK result types):

- Use `when` guard clauses generously since you cannot add `Deconstruct` methods without extension methods or wrapper types
- Prefer wrapping uncontrolled types in a thin domain record before pattern matching: `record ApiResult(bool IsSuccess, int StatusCode, string? ErrorMessage)` -- this converts the external type to a shape you control
- Do not use type patterns on `Exception` subclasses in a switch expression that catches all `Exception` -- it will suppress exceptions that should propagate. Always include a re-throw arm: `_ => throw new UnreachableException("Unhandled exception type", ex)`

### Primary Constructors and Serialization Frameworks

Primary constructors on non-record classes introduce an implicit captured scope, not a named parameter set:

- `System.Text.Json` does not see primary constructor parameters as constructor parameters for deserialization -- the generated IL does not emit them as a named constructor in the way positional record constructors do. Use a traditional constructor with `[JsonConstructor]` if deserialization from JSON is required.
- EF Core 8 supports primary constructors on entity types, but only when the parameter names exactly match the property names (case-insensitive). If using primary constructors for EF Core entities, ensure the parameter name matches the property it initializes: `public class Product(int id, string name)` with `public int Id { get; } = id; public string Name { get; } = name;`.

### Mixed Record and Class Hierarchies

C# enforces that records can only inherit from other records and classes can only inherit from classes. This becomes a constraint when:

- You have a base domain class that you want to convert to a record but derived classes are plain classes -- you must convert the entire hierarchy at once or not at all
- You try to use a record as a base for an EF Core entity (not supported)
- A third-party library defines a base class you must inherit from -- you cannot make the derived type a record. Use `required init` properties with manual `Equals`/`GetHashCode` override as a substitute

### List Patterns on Non-Array Enumerables

List patterns (`[first, .., last]`) only work on types that implement `IReadOnlyList<T>` (for `Count` and indexer) or arrays. They do not work directly on `IEnumerable<T>`:

- `IEnumerable<T>` requires `.ToArray()` or `.ToList()` before applying list patterns -- add a comment explaining why the materialization is intentional
- `Span<T>` and `ReadOnlySpan<T>` support list patterns in C# 11+ without heap allocation
- For streaming/lazy sequences, use `foreach` with early break rather than list patterns -- materializing a lazy sequence to apply a list pattern defeats the purpose of the lazy evaluation

### `with` Expressions and Deep Object Graphs

`with` expressions perform a shallow copy of a record:

- If a record contains a reference-type property (`List<OrderItem> Items`), the `with` copy shares the same `List<T>` instance. Mutating the list in one record affects the other.
- For deep immutability, either use `ImmutableArray<T>` or `ImmutableList<T>` as property types, or wrap the `with` expression in a factory method that performs the deep copy explicitly.
- Document the shallow-copy behavior in XML doc comments on any record that holds reference-type collection properties: `/// <remarks>The Items collection is not deep-copied by with expressions.</remarks>`

---

## Example

**Input:** "I have a C# 12 project. Here is my order processing code -- it has a big if/else chain for order status, a DTO class with 8 properties and hand-written Equals, and I'm getting nullable warnings everywhere. How do I modernize it?"

**Diagnosis:**

The code has three separate modernization targets:
1. A `class OrderDto` with 8 properties, hand-written `Equals`/`GetHashCode`, and `ToString` -- a direct candidate for `record class`
2. An `if`/`else if` chain over `OrderStatus` enum values -- a direct candidate for a switch expression with exhaustiveness checking
3. Uncontrolled nullable reference type warnings from not having `<Nullable>enable</Nullable>` configured

---

**Before:**

// OrderDto.cs -- old style

using System;

using System.Collections.Generic;

namespace MyApp.Orders

{

public class OrderDto

{

public Guid Id { get; set; }

public string CustomerName { get; set; }

public string CustomerEmail { get; set; }

public DateTime PlacedAt { get; set; }

public decimal Total { get; set; }

public string Status { get; set; }

public string ShippingAddress { get; set; }

public List<string> ItemSkus { get; set; }

public override bool Equals(object obj)

{

if (obj is OrderDto other)

return Id == other.Id && CustomerName == other.CustomerName;

return false;

}

public override int GetHashCode() => HashCode.Combine(Id, CustomerName);

public override string ToString() =>

$"Order {Id} for {CustomerName} ({Status})";

}

}

// OrderProcessor.cs -- old style

using System;

namespace MyApp.Orders

{

public class OrderProcessor

{

private readonly IOrderRepository _repository;

private readonly IEmailService _emailService;

public OrderProcessor(IOrderRepository repository, IEmailService emailService)

{

_repository = repository;

_emailService = emailService;

}

public string GetStatusMessage(string status)

{

if (status == "Pending")

return "Your order is being reviewed.";

else if (status == "Processing")

return "Your order is being prepared.";

else if (status == "Shipped")

return "Your order is on its way.";

else if (status == "Delivered")

return "Your order has been delivered.";

else if (status == "Cancelled")

return "Your order was cancelled.";

else

return "Unknown status.";

}

}

}


---

**After:**

// GlobalUsings.cs -- project root, used in 10+ files

global using System;

global using System.Collections.Generic;

global using System.Collections.Immutable;

// OrderStatus.cs

namespace MyApp.Orders; // file-scoped namespace -- removes one indentation level

public enum OrderStatus

{

Pending,

Processing,

Shipped,

Delivered,

Cancelled

}

// OrderDto.cs -- modernized as a sealed record class

namespace MyApp.Orders;

/// <summary>

/// Immutable data transfer object representing a placed order.

/// Value equality is by all properties (record default).

/// </summary>

/// <remarks>

/// ItemSkus uses ImmutableArray to ensure with-expression copies

/// do not share a mutable list reference.

/// </remarks>

public sealed record OrderDto

{

// required + init enforces completeness at the call site without

// a constructor with 8 positional parameters (which would be

// a transposition bug waiting to happen).

public required Guid Id { get; init; }

public required string CustomerName { get; init; }

public required string CustomerEmail { get; init; }

public required DateTime PlacedAt { get; init; }

public required decimal Total { get; init; }

public required OrderStatus Status { get; init; }

public required string ShippingAddress { get; init; }

// ImmutableArray<T> -- deep-copy safe with with-expressions,

// stack-allocated wrapper, zero extra heap allocation.

public ImmutableArray<string> ItemSkus { get; init; } = ImmutableArray<string>.Empty;

// No need to override Equals, GetHashCode, or ToString --

// the record generates all three correctly.

// Generated ToString: OrderDto { Id = ..., CustomerName = ..., ... }

}

// OrderProcessor.cs -- modernized with primary constructor and switch expression

namespace MyApp.Orders;

public sealed class OrderProcessor(

IOrderRepository repository, // primary constructor -- captured in class scope

IEmailService emailService, // no need for separate _field = field assignments

ILogger<OrderProcessor> logger) // DI parameters read naturally top-to-bottom

{

// Primary constructor parameters (repository, emailService, logger) are

// accessible throughout the class body -- they are implicitly captured.

// Typed enum instead of raw string -- callers cannot pass arbitrary strings.

public string GetStatusMessage(OrderStatus status) =>

status switch // switch expression -- no fall-through

{

OrderStatus.Pending => "Your order is being reviewed.",

OrderStatus.Processing => "Your order is being prepared.",

OrderStatus.Shipped => "Your order is on its way.",

OrderStatus.Delivered => "Your order has been delivered.",

OrderStatus.Cancelled => "Your order was cancelled.",

// Exhaustiveness: if a new enum value is added, this arm catches it

// at runtime and throws immediately -- never silently returns a wrong string.

_ => throw new UnreachableException($"Unhandled OrderStatus: {status}")

};

public async Task ProcessAsync(OrderDto order, CancellationToken ct = default)

{

// ArgumentNullException.ThrowIfNull is the .NET 6+ idiomatic null guard.

// Nullable reference types mean the compiler already warns if order could

// be null -- this guard is for runtime contract enforcement.

ArgumentNullException.ThrowIfNull(order);

// Property pattern matching -- reads as English, no temp variable needed.

if (order is { Status: OrderStatus.Pending, Total: > 0m })

{

await repository.SaveAsync(order, ct);

// Immutable update via with-expression -- order is unchanged,

// updatedOrder is a new record with only Status modified.

var updatedOrder = order with { Status = OrderStatus.Processing };

logger.LogInformation(

"Order {OrderId} transitioned to {Status}",

updatedOrder.Id,

updatedOrder.Status);

await emailService.SendAsync(updatedOrder.CustomerEmail, ct);

}

}

}


---

**Key Idioms Applied:**

| Idiom | C# Version | Benefit | Trade-off |
|-------|-----------|---------|-----------|
| `sealed record` with `required init` | 9 + 11 | Eliminates hand-written Equals/GetHashCode/ToString, enforces completeness | Slightly more syntax than positional records; safe for types with many same-type parameters |
| `ImmutableArray<T>` as collection property | Any | `with` expressions are deep-copy safe; no shared mutable reference | `ImmutableArray` is a struct -- use `.IsDefaultOrEmpty` not `== null` |
| File-scoped namespace | 10+ | Removes one indentation level across every file in project | Only one namespace per file allowed |
| Global usings | 10+ | Removes 3 boilerplate `using` lines from every file | Namespace conflicts can hide ambiguity |
| Switch expression over typed enum | 8+ | Compiler exhaustiveness warning when enum is extended | `UnreachableException` arm is required for true exhaustiveness enforcement |
| Property pattern (`{ Status: ..., Total: > 0m }`) | 8+ | Replaces multi-line null + property checks | Requires team familiarity to read confidently |
| Primary constructor for DI | 12 | Cleaner DI registration, no field boilerplate | Parameters are not properties; no validation body |
| `with` expression for immutable update | 9+ | Produces a new instance with one field changed | Shallow copy -- requires `ImmutableArray<T>` for collection safety |
| `ArgumentNullException.ThrowIfNull` | .NET 6+ | Short, throw-annotated, nullable-flow aware | Not available below .NET 6; use manual check there |

---

**Migration Guidance:**

1. Add `<Nullable>enable</Nullable>` and `<LangVersion>12.0</LangVersion>` to `.csproj`
2. Add `<ImplicitUsings>enable</ImplicitUsings>` and create `GlobalUsings.cs` with namespaces used in 5+ files
3. Run `dotnet build` and capture nullable warning count: `dotnet build 2>&1 | grep -c "warning CS8"`
4. For each file modified in the current PR: convert namespace to file-scoped, address nullable warnings
5. Convert `OrderDto` from class to `sealed record` -- run serialization tests immediately after
6. Replace `string status` parameter with `OrderStatus status` enum across all call sites
7. Convert `if`/`else if` chains to switch expressions one method at a time -- verify with existing unit tests after each conversion
8. Convert `OrderProcessor` constructor to primary constructor -- confirm DI container registration still resolves correctly

---

**Tooling Configuration:**

<!-- In .csproj -->

<PropertyGroup>

<LangVersion>12.0</LangVersion>

<Nullable>enable</Nullable>

<ImplicitUsings>enable</ImplicitUsings>

<!-- Uncomment for greenfield projects only: -->

<!-- <WarningsAsErrors>Nullable</WarningsAsErrors> -->

</PropertyGroup>

In .editorconfig

[*.cs]

csharp_style_namespace_declarations = file_scoped:warning

csharp_style_prefer_switch_expression = true:suggestion

csharp_prefer_pattern_matching = true:suggestion

csharp_style_prefer_null_check_over_type_check = true:warning

dotnet_style_prefer_auto_properties = true:suggestion

csharp_style_prefer_primary_constructors = true:suggestion

dotnet_diagnostic.CS8600.severity = warning # nullable assignment

dotnet_diagnostic.CS8602.severity = warning # nullable dereference

dotnet_diagnostic.CS8618.severity = warning # non-nullable uninitialized

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.