C
C#11mo 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
Angius11mo ago
.OfType<>() maybe?
canton7
canton711mo ago
Yep, this is the answer.
Denis
Denis11mo ago
Use /close if the issue is resolved
BenMcLean
BenMcLean11mo ago
Thanks
Want results from more Discord servers?
Add your server