A struct with an interface is inefficient in .NET

A struct with an interface is inefficient in .NET

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.

BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19044.2486/21H2/November2021Update)
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK=7.0.200-preview.22628.1
  [Host]     : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2
  DefaultJob : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT AVX2


|              Method |      Mean |     Error |    StdDev |    Median |   Gen0 | Allocated |
|-------------------- |----------:|----------:|----------:|----------:|-------:|----------:|
|               Class | 2.9964 ns | 0.0713 ns | 0.0667 ns | 3.0374 ns | 0.0014 |      24 B |
|  ClassWithInterface | 3.0590 ns | 0.1043 ns | 0.0976 ns | 3.0742 ns | 0.0014 |      24 B |
|              Struct | 0.0018 ns | 0.0030 ns | 0.0023 ns | 0.0012 ns |      - |         - |
| 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.

Sustainable Code - by BEN ABT