1

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?

l33t
  • 18,692
  • 16
  • 103
  • 180

0 Answers0