If I have a generic function that returns a T
or some default value I want to use the ??
operator
T f<T>(T a, T b)
{
return a ?? b;
}
This fails with the message
Operator '??' cannot be applied to operands of type 'T' and 'T'
I assume this is because T
might not be nullable. But I would also assume that the ??
operator above should do the same as
T f<T>(T a, T b)
{
var a_ = a;
return a_ != null ? a_ : b;
}
And the second code compiles without a problem.
So why are those cases handled differently even though they should do the same?
From this question on dotnet/csharplang:
The reason for this is that ?? is intended to unwrap the type on the left if the type on hte left is
Nullable<T>
. In other words, today i can write:int? x ...; int y ...; int z = x ?? y;
So, it's not the case that "x ?? y" translates to "x != null ? x : y". It can translate to "x != null ? x.Value : y".
Since we don't know if T is a value or reference type, we cannot effectively do the transformation. However, when T is constrained to "class" or "struct" (and in the latter case, is a T?), then we can do the transformation.
See the rest of that thread for a wider discussion.
There's a discussion around relaxing this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With