dotnet-csharp-type-design-performance

Design high-performance .NET 8 types with structs, classes, and collections.

Updated Aug 16, 2025
One-click install
npx skills add https://github.com/dodyg/blue-nile-pds --skill dotnet-csharp-type-design-performance-dodyg
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-csharp-type-design-performance
Source: https://github.com/dodyg/blue-nile-pds/tree/main/.agents/skills/dotnet-csharp-type-design-performance
Command: npx skills add https://github.com/dodyg/blue-nile-pds --skill dotnet-csharp-type-design-performance-dodyg

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Designing .NET types such as structs, classes, sealed classes, readonly structs, Span/Memory usage, and collection choices incorrectly can cause unnecessary allocations, slower execution, and maintenance challenges in high‑performance applications.

Core Features & Use Cases

  • Decision matrix for struct versus class selection based on size, lifetime, identity, and mutability.
  • Guidelines for sealing library types by default to enable JIT devirtualization.
  • Checklist for creating readonly structs and using in‑parameters to avoid defensive copies.
  • Advice on ref struct constraints and when to choose Span<T> versus Memory<T>.
  • Recommendations for collection types including FrozenDictionary, ImmutableDictionary, and concurrent collections for different usage patterns.

Quick Start

Generate a concise type‑design checklist for a new .NET 8 library focusing on structs, sealing, readonly structs, Span/Memory, and collection choices.

Frequently Asked Questions about dotnet-csharp-type-design-performance

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
When should I use a struct versus a class in C# for high performance?

C# type design requires sealing library types by default to enable JIT devirtualization. Sealing prevents inheritance overhead, allowing the runtime to optimize method calls and improve execution speed in high-performance applications.

How do I avoid defensive copies of readonly structs in .NET?

Avoid defensive copies of readonly structs by using the in-parameter modifier. This passes arguments by reference while enforcing immutability, preventing the compiler from generating hidden copies during method calls.

Should I use Span<T> or Memory<T> for my .NET type design?

Choose Span<T> over Memory<T> for synchronous, high-performance memory operations because ref struct constraints prevent boxing and heap allocation. Use Memory<T> when you need to store memory regions in fields or across asynchronous boundaries.

What is the best .NET collection for immutable data that rarely changes?

FrozenDictionary is the best .NET collection for immutable data built once and read frequently. Unlike ImmutableDictionary, it optimizes read operations at the cost of fast mutations, making it ideal for application lifetime data.

What are the limitations of using ref struct in C# library development?

Ref struct limitations in C# include inability to be boxed, captured by lambdas, stored in fields, or used across async boundaries. These constraints ensure stack-only allocation but restrict flexibility in type design.