/dotnet-agent-kit:modern-csharpfrom dotnet Agent Kit
Guides use of modern C# 14 / .NET 10 features: primary constructors, collection expressions, field keyword, records, pattern matching, spans, raw string literals. Activates for new C# code or modernization tasks.
Ships in dotnet Agent Kit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
record struct, Span<T>, and stack allocation to reduce GC pressure.record, readonly, init, and required to make illegal states unrepresentable.| Feature | Usage | Example |
|---|---|---|
| Primary constructors | DI injection, eliminate field assignments | public class OrderService(IOrderRepo repo, TimeProvider clock) { } |
| Collection expressions | [] for all collection types + spread | List<string> names = ["Alice", "Bob"]; / int[] all = [..a, ..b, 99]; |
| Records | DTOs, value objects, immutable data | public record CreateOrderRequest(string CustomerId, List<OrderItem> Items); |
readonly record struct | Small stack-allocated value types | public readonly record struct Money(decimal Amount, string Currency); |
| Pattern matching | Switch expressions, list/property patterns | order switch { { Total: > 1000 } => "Premium", _ => "Standard" }; |
| List patterns | Deconstruct arrays/lists | items switch { [] => "Empty", [var x] => $"One: {x}", [var f, .., var l] => $"{f}..{l}" }; |
Span<T> | Zero-allocation slicing | ReadOnlySpan<char> trimmed = input.Trim(); int.TryParse(trimmed[4..], out id); |
| Raw string literals | Multi-line SQL, JSON, XML | var sql = """ SELECT ... """; / interpolated: $$""" {"id": "{{id}}"} """; |
required members | Enforce initialization | public required string ConnectionString { get; init; } |
is pattern + extraction | Null/type/property check | if (result is { IsSuccess: true, Value: var order }) { ... } |
field Keyword (C# 14)Access the auto-generated backing field in property accessors without declaring it manually.
// GOOD — field keyword for validation in auto-property
public class Product
{
public string Name
{
get => field;
set => field = value?.Trim() ?? throw new ArgumentNullException(nameof(value));
}
public decimal Price
{
get => field;
set => field = value >= 0 ? value : throw new ArgumentOutOfRangeException(nameof(value));
}
}
fieldpublic class ProductCatalog
{
// Lazy-load on first access — no manual Lazy<T> or backing field
public IReadOnlyList<Product> Products
{
get => field ??= LoadProducts();
}
private static List<Product> LoadProducts() => /* expensive load */;
}
field// INotifyPropertyChanged without manual backing fields
public class OrderViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public string CustomerName
{
get => field;
set
{
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CustomerName)));
}
} = "";
public decimal Total
{
get => field;
set
{
if (field == value) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Total)));
}
}
}
C# 14 adds extension blocks inside static classes. Unlike classic extension methods, they support extension properties and static extension members — the receiver is declared once for the whole block.
// GOOD — extension block (shipped C# 14 syntax)
public static class OrderExtensions
{
extension(Order order)
{
public decimal TotalWithTax => order.Total * 1.2m;
public bool IsHighValue => order.Total > 1000m;
public string ToSummary() =>
$"Order #{order.Id}: {order.Total:C} ({order.Items.Count} items)";
}
// Static extension members use the type (no receiver instance)
extension(Order)
{
public static Order Empty => Order.Create("none", [], DateTimeOffset.MinValue);
}
}
// Callers see them as if declared on Order
if (order.IsHighValue) { /* ... */ }
Classic this-parameter extension methods still work and coexist — use extension blocks when you need properties or several members on the same receiver.
Requires
net11.0/ C# 15. Do not generate these innet10.0projects; they will not compile. C# 14 stays the baseline. Move to net11.0 only when you need unions or runtime async (seeknowledge/dotnet-whats-new.md).
A union declares a closed set of case types with implicit conversions and exhaustive switch.
// BAD (net11.0) — open hierarchy, switch needs a default arm that hides new cases
public abstract record Pet;
public sealed record Cat(string Name) : Pet;
public sealed record Dog(string Name) : Pet;
string Describe(Pet p) => p switch { Cat c => c.Name, Dog d => d.Name, _ => "?" };
// GOOD (net11.0) — union; compiler checks the switch is exhaustive
public union Pet(Cat, Dog, Bird);
string Describe(Pet p) => p switch { Cat c => c.Name, Dog d => d.Name, Bird b => b.Name };
System.Text.Json supports C# unions in .NET 11. For expected failures, prefer a union over exceptions (see the error-handling rules).
closed hierarchiesUse closed when cases need shared members or state that a union cannot carry. Direct descendants are fixed at compile time, so switches need no default arm.
// GOOD (net11.0)
public closed record PaymentResult;
public sealed record Paid(string Receipt) : PaymentResult;
public sealed record Declined(string Reason) : PaymentResult;
// GOOD (net11.0) — indexer in an extension block
public static class QueueExtensions
{
extension<T>(IReadOnlyList<T> list)
{
public T this[Index i] => list[i.GetOffset(list.Count)];
}
}
// BAD — must fall back to a constructor to set capacity or comparer
var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "a", "b" };
// GOOD (net11.0) — arguments go inside the collection expression
HashSet<string> names = [with(StringComparer.OrdinalIgnoreCase), "a", "b"];
C# 15 also adds labeled break / continue for leaving an outer loop without flag variables.
// BAD — manual backing field when field keyword works
private string _name;
public string Name
{
get => _name;
set => _name = value ?? throw new ArgumentNullException();
}
// BAD — old-style collection initialization
var list = new List<int>() { 1, 2, 3 };
// BAD — Tuple instead of record for domain types
(string Name, decimal Price) product = ("Widget", 9.99m);
// GOOD — record
public record Product(string Name, decimal Price);
// BAD — deeply nested pattern that's hard to read
if (order is { Customer: { Address: { Country: { Code: "US" } } } })
// GOOD — extract to a clear method or use sequential checks
if (order.Customer.Address.Country.Code == "US")
var When the Type Is Not Obvious// BAD — what type is this?
var result = Process(order);
// GOOD — explicit type when not obvious
Result<Order> result = Process(order);
// Also GOOD — var is fine when type is apparent
var orders = new List<Order>();
| Scenario | Recommendation |
|---|---|
| DTO / API contract | record (reference type) |
| Small value object (2-3 fields) | readonly record struct |
| Service with DI | Primary constructor |
| Collection creation | Collection expression [] |
| Property with validation | field keyword |
| Multi-line string (SQL, JSON) | Raw string literal """ |
| Slicing strings/arrays | Span<T> |
| Type checking + extraction | Pattern matching with is / switch |
| Enforced initialization | required modifier |
| Adding methods to external types | Extension members |
| Closed set of cases (net11.0 only) | C# 15 union or closed hierarchy |
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub codewithmukesh/dotnet-agent-kit --plugin dotnet-agent-kit37 skills in this plugin
/api-hardeningProduction hardening for ASP.NET Core 10 APIs: HSTS, forwarded headers behind a proxy (KnownIPNetworks), strict CORS, body and upload limits, security headers, no detailed errors. Use for "HSTS", "forwarded headers", "behind nginx", "CORS", "request size limit", "security headers", "harden for production". Do NOT use for auth (use authentication) or throttling (use rate-limiting).
/architecture-advisorArchitecture selection for new or restructured .NET solutions: asks about domain, team and compliance, then recommends modular slices (default), vertical slice, Clean Architecture or DDD. Use for "which architecture", "new solution", "restructure", "DDD", "project structure", "tech stack". Do NOT use for adding features to a modular-slices codebase (modular-slices), scaffolding (scaffold), or CLAUDE.md (dotnet-init).
/Other plugins' skills for the same job