type-design-performance

Guide .NET type design for performance with struct versus class decisions.

71|10|Updated Feb 11, 2026
One-click install
npx skills add https://github.com/wshaddix/dotnet-skills --skill type-design-performance-wshaddix
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: type-design-performance
Source: https://github.com/wshaddix/dotnet-skills/tree/main/skills/csharp-type-design-performance
Command: npx skills add https://github.com/wshaddix/dotnet-skills --skill type-design-performance-wshaddix

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you make informed decisions about designing .NET types to maximize performance, reducing memory allocations and improving execution speed.

Core Features & Use Cases

  • Struct vs. Class Decisions: Provides a matrix to guide the choice between value types (structs) and reference types (classes).
  • Immutability and Readonly: Emphasizes the benefits of readonly structs and sealed classes.
  • Efficient Collections: Guides the selection of appropriate collection types like FrozenDictionary and IReadOnlyList.
  • Use Case: When designing a new Point struct, this skill helps determine if it should be a readonly struct for performance or a class if inheritance is needed.

Quick Start

Use the type-design-performance skill to decide whether to use a struct or a class for a new Money type.

Frequently Asked Questions about type-design-performance

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

FAQPage Schema
When should I use a struct vs a class in .NET for performance?

Use a readonly struct for small, immutable data payloads to avoid allocation overhead and prevent defensive copying. Use a class when the type requires inheritance, polymorphism, or represents a large object graph where reference semantics are more efficient.

How do I optimize .NET collection choices for high performance?

Optimize .NET collections by selecting FrozenDictionary for immutable lookup-heavy scenarios and IReadOnlyList for exposing data safely. Choosing the appropriate collection type minimizes memory overhead and maximizes execution speed during iteration and retrieval operations.

What is the benefit of making a struct readonly in .NET?

Making a struct readonly in .NET prevents the compiler from emitting defensive copies when the struct is accessed, reducing memory overhead and improving execution speed. It enforces immutability, ensuring the type's state cannot be modified after creation.

Should I seal my .NET classes by default to improve performance?

Sealing .NET classes by default improves performance by enabling compiler optimizations and devirtualization. It prevents unwanted inheritance chains, allowing the runtime to bypass virtual method table lookups and execute method calls more efficiently.

Can I use Span<T> and Memory<T> to reduce allocations in .NET type design?

Span<T> and Memory<T> reduce allocations in .NET type design by providing safe, managed pointers to contiguous memory regions. They enable high-performance processing of arrays or strings without copying data, maximizing execution speed and minimizing memory overhead.

What are the limitations of using structs for .NET type design?

Structs in .NET type design carry limitations including boxing overhead when cast to interfaces and potential performance degradation from defensive copying if not marked readonly. They lack support for inheritance, making them unsuitable for complex object hierarchies.