C
C#2y ago
DaVinki

✅ 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
DaVinki
DaVinkiOP2y ago
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?
333fred
333fred2y ago
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 conversion
DaVinki
DaVinkiOP2y ago
That makes sense, alright
DaVinki
DaVinkiOP2y ago
Thanks
333fred
333fred2y ago
yw Make sure to $close the thread if you don't have any other questions
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server