✅ Using Linq's Cast Method
I've never been able to get it to not warn and throw errors except for one, maybe two times. It is always warning me even if the cast is what I want and has thrown over it before. These casts are trivial casts like casting an int to another value type like long or char. I also have never really bothered asking about it since I just do
.Select(o => (T)o)
in the end.
Say I had an IEnumerable of random ints. Why can't I just do .Cast<char>()
if I wanted to cast them into chars instead of doing .Select(i => (char)i)
? If I can, what am I missing to do so?7 Replies
The warn is
Suspicious cast: there is no type in the solution which is inherited from both 'int' and 'char'
. Is it just not possible to do unsuspiciously with value types using this method?From the remarks of
Cast
:
The only type conversions that are performed by this method are reference conversions and unboxing conversions.
int
to char
is a numeric conversion, not a reference or unboxing conversionThat makes sense, alright
Thanks
yw
Make sure to $close the thread if you don't have any other questions
Use the
/close
command to mark a forum thread as answered