C
C#13mo 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
Angius13mo ago
.OfType<>() maybe?
canton7
canton713mo ago
Yep, this is the answer.
Denis
Denis13mo ago
Use /close if the issue is resolved
BenMcLean
BenMcLeanOP13mo ago
Thanks

Did you find this page helpful?