
Structs have their advantages in .NET: they are especially efficient in the runtime if used correctly.
But if you use structs with an interface, the massive advantages unfortunately turn into disadvantages:
As soon as an interface is attached to a struct, Boxing
comes into effect; the values are thus copied over and over again, which is usually not what you want - but it also shows up in the benchmarks.
1BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2486/21H2/November2021Update)
2AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
3.NET SDK=7.0.200-preview.22628.1
4 [Host] : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2
5 DefaultJob : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2
6
7
8| Method | Mean | Error | StdDev | Median | Gen0 | Allocated |
9|-------------------- |----------:|----------:|----------:|----------:|-------:|----------:|
10| Class | 2.9964 ns | 0.0713 ns | 0.0667 ns | 3.0374 ns | 0.0014 | 24 B |
11| ClassWithInterface | 3.0590 ns | 0.1043 ns | 0.0976 ns | 3.0742 ns | 0.0014 | 24 B |
12| Struct | 0.0018 ns | 0.0030 ns | 0.0023 ns | 0.0012 ns | - | - |
13| StructWithInterface | 3.1241 ns | 0.0835 ns | 0.0781 ns | 3.1147 ns | 0.0014 | 24 B |
In principle, this behavior is nothing new. Potential solutions are already being worked dotnet/csharplang - Proposal: Allow readonly ref structs to implement interfaces, but disallow boxing them #1479 on so that structs can be used boxing-free.
Related articles

Feb 23, 2023 - 2 min read
Difference of is null vs equals null and why is null is recommended
Both C# is null and == null are used to check if a variable or object is null, but they work slightly differently.

Feb 02, 2023 - 2 min read
Create Symbolic Links on Windows with .NET
Symbolic Links (often abbreviated as symlinks) are a type of file or folder shortcut in Windows that reference another file or folder in the …

Jan 30, 2023 - 1 min read
Benefits of using isolated mode of Azure Functions with .NET
The isolated mode of Azure Functions with .NET allows for greater control over the runtime environment and dependencies of the function. In …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
