C#C
C#3y 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>())

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?
Was this page helpful?