dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Allow ref struct on Generic ThrowHelper methods

Open Avid29 opened this issue 5 months ago • 0 comments

Overview

This would allow ref structs to be used in pattern matching with a ThrowHelper case, using the new allow ref struct "anti-constraint".

API breakdown

This changes nothing except allowable use cases for existing ThrowHelper APIs.

Usage example

I encountered a situation in my code where I wanted to pattern match and return a ref struct. I found I could not use the ThrowHelper because it was a ref struct.

Here's an example of another imaginable situation utilizing the Span<T>.

Span<int> span = x switch
{
    0 => stackalloc int[size],
    1 => new int[size],
    2 => something.AsSpan(),
    _ => ThrowHelper.ThrowArgumentOutOfRangeException<Span<int>>(),
};

Breaking change?

No

Alternatives

throw without ThrowHelper

Additional context

This will require .NET 9 and C# version 13

Help us help you

Yes, I'd like to be assigned to work on this item

Avid29 avatar Oct 15 '25 02:10 Avid29