Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ versus yield

Tags:

yield

c#

.net

linq

I almost don't use "yield" operator (I don't hate it :)). I prefer to use LINQ if it is possible. Anyways, I looked for some post (you can find link below) 10 minutes ago, read it and some idea visited my brain :)

rewrite-this-foreach-yield-to-a-linq-yield

Idea: Probably, it is not very good that I don't use "yield". Probably, it has better performance than LINQ or some other advantage.

Therefore I have next question, what code is more "correct" (yield or LINQ) in example above (in common case)?

P.S. I am interesting for cases when we have possibility to use LINQ instead of "yield".

like image 556
Maxim Polishchuk Avatar asked Dec 22 '22 07:12

Maxim Polishchuk


1 Answers

I think it's clearer to use LINQ in that case, personally. It's operating at a higher level than "call this method, yield this result" - it describes the overall results.

Iterator blocks are very useful for implementing LINQ though - either the existing operators, or adding your own. It's worth knowing about them, but I wouldn't worry about the fact that you're not using them much - that's not a sign of poor code or anything like that.

like image 106
Jon Skeet Avatar answered Jan 11 '23 00:01

Jon Skeet