❔ Casting range to bytes?
The result:
System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Byte'.
Why?
This should work!
Why doesn't this work!?8 Replies
I know that this is a valid cast, because this should be equivalent to that:
and yet using
Select
works just fine.It's so sad that this isn't a thing
But it doesn't work because it doesn't do explicit casting like you do in your Select
It uses the
is
keyword, guaranteeing no loss of data
If it's not to an inherited type, it won't work
For this instance, int
is not byte
But given any reference type, say Rectangle
, that is object
Oh, that makes sense. The
Cast
method only works if the type being cast from is
the type being cast to
ThanksOr if you had a stream of boxed objects of type
object
, if you know 100% they are all Rectangle
, you can do .Cast<Rectangle>()
But yeah it's a shame there is no casting method that allows casting numbers that isn't selectdaVinki#5278
REPL Result: Success
Console Output
Compile: 855.536ms | Execution: 168.607ms | React with ❌ to remove this embed.
because it effectively does
that cast there is an unboxing conversion, which can't do this
Windows10CE#8553
REPL Result: Failure
Exception: InvalidCastException
Compile: 477.088ms | Execution: 29.689ms | React with ❌ to remove this embed.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.