C
C#12mo ago
BenMcLean

✅ Safe type casting with LINQ?

As part of an octree I'm implementing, I have this line:
foreach (Leaf leaf in popped.Where(node => node is Leaf).Cast<Leaf>())
foreach (Leaf leaf in popped.Where(node => node is Leaf).Cast<Leaf>())
popped enumerates type Node. Type Leaf is a child class of Node. My question is, can I do this Where and Cast in a single operation which is type safe or does that line look correct as-is?
4 Replies
Angius
Angius12mo ago
.OfType<>() maybe?
canton7
canton712mo ago
Yep, this is the answer.
Denis
Denis12mo ago
Use /close if the issue is resolved
BenMcLean
BenMcLeanOP12mo ago
Thanks
Want results from more Discord servers?
Add your server