CS8121: Custom <T> cannot be processed by pattern type 'T'

Given the following code:

struct Custom<T> where T : struct { }
//...
Nullable<int> x = 2;
if(x is int z) { } //compiles
Custom<int> a = 2;
if(a is int b) { } //CS8121 An expression of type 'Custom<int>' cannot be handled by a pattern of type 'int'.

What allows you to Nullable<T>process the template T? Can I do the same for my custom structure? I already tried conversion operators, but no luck. Not sure if I tried all possible operators.

+4
source share
1 answer

What allows you to Nullable<T>process the template T?

The magic of the compiler. There is no specific conversion operator that allows this to work (I tested), it's just a compiler that knows how to map the template Nullable<T>to T.

+4
source

Source: https://habr.com/ru/post/1691790/


All Articles