Accepted answer

ienumerable<element>.cast<someclass>().firstordefault(); is probably want you want, but the answer has nothing to do with performance.

unless element is ienumerable, it doesn't make sense to use cast() on the result of firstordefault(), and will result in a compile error.

assuming you meant (someclass)(ienumerable<element>.firstordefault()); for your second case, this is also problematic because if ienumerable<element> is empty, then firstordefault() will return null if element is a reference type, or default construct element if it's a value type. it would be preferable to default construct someclass in the event that ienumerable<element> is empty and someclass is a value type, rather than attempting to cast from element to someclass.

ienumerable<element>.cast<someclass>().firstordefault(); will only ever perform a maximum of one cast from element to someclass because linq chains are lazily enumerated.

Related Query