CommunityToolkit / CommunityToolkit/dotnet
Allow ref struct on Generic ThrowHelper methods
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Overview
This would allow ref structs to be used in pattern matching with a `ThrowHelper` case, using the new [allow ref struct](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#allows-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`.
```cs
Span span = x switch
{
0 => stackalloc int[size],
1 => new int[size],
2 => something.AsSpan(),
_ => ThrowHelper.ThrowArgumentOutOfRangeException>(),
};
```
### 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
Contributor guide
Assessment
This issue has not been assessed yet.