© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
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
Node
. Type
Leaf
Leaf
is a child class of
Node
Node
.
My question is, can I do this
Where
Where
and
Cast
Cast
in a single operation which is type safe or does that line look correct as-is?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Type Casting
C#CC# / help
4y ago
Help with Linq
C#CC# / help
4y ago
✅ Casting from one generic type to another
C#CC# / help
4y ago
❔ XMLAttributeCollection -> Dictionary with LINQ?
C#CC# / help
3y ago