Targeting .NET 6
. Consider this simple tuple-cast:
(string, bool) x = ("", false);
var ok = ((string, bool?))x; // Valid!
Console.WriteLine(ok);
Enumerable.Repeat(x, 1)
.Cast<(string, bool?)>().ToList(); // Invalid?!
The call to Cast<T>()
throws:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.ValueTuple2[System.String,System.Boolean]' to type 'System.ValueTuple2[System.String,System.Nullable`1[System.Boolean]]'.
What's up with this Cast<T>()
method? Why can't it cast the value?