dotnet-csharp-type-design-performance

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

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses critical upfront type design choices in .NET that significantly impact application performance and maintainability throughout their lifecycle.

Core Features & Use Cases

  • Struct vs. Class Decision Matrix: Guides the selection between struct and class based on size, lifetime, and identity.
  • readonly struct: Eliminates defensive copies for immutable value types.
  • ref struct and Span<T>/Memory<T>: Optimizes memory access for stack-allocated or contiguous data.
  • Collection Type Selection: Recommends FrozenDictionary, ImmutableDictionary, ConcurrentDictionary, and Dictionary based on usage patterns.
  • Use Case: When designing a new data structure for high-frequency calculations, this Skill helps determine if a readonly struct or a class is more appropriate, considering memory allocation and copy costs.

Quick Start

Use the dotnet-csharp-type-design-performance skill to understand when to use a struct versus a class for a new data type.

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 vs a class in C# for better performance?

Choosing between a struct and a class in C# depends on size, lifetime, and identity. Structs avoid heap allocation overhead for small, short-lived data, while classes are better for larger objects requiring reference semantics and shared mutability.

What are the performance benefits of using a readonly struct in .NET?

Using a readonly struct in .NET eliminates defensive copies when passing value types to methods. This ensures immutability while reducing unnecessary memory allocations and CPU overhead during high-frequency calculations.

How do ref structs and Span<T> optimize memory access in C#?

Ref structs and Span<T> optimize memory access by enabling stack-allocated or contiguous data manipulation without heap allocations. This minimizes garbage collection pressure and improves throughput for high-performance data processing.

What's the best way to choose between FrozenDictionary and ConcurrentDictionary in .NET?

Choose FrozenDictionary for immutable, lookup-heavy data created once, ConcurrentDictionary for thread-safe concurrent updates, and ImmutableDictionary for immutable updates. Dictionary remains the standard for general-purpose mutable key-value storage.

Does type design impact memory allocation costs in .NET 8.0 applications?

Type design directly impacts memory allocation costs in .NET 8.0 applications. Selecting appropriate structs, readonly structs, or ref structs minimizes heap allocations, reduces garbage collection cycles, and lowers overall copy costs.

What are the limitations of using ref structs for type design in C#?

Ref structs in C# are confined to the stack and cannot be boxed, stored as fields in classes, or used across async boundaries. These limitations ensure safe memory access but restrict their use to narrow, high-performance scopes.