DEV Community

C# lambda vs foreach readability.

Ulisses Cavalcante on June 30, 2021

Hey guys, recently a receive an feedback that i need to change my code, turn an lambda expression into a foreach loop because its more adherent to ...
Collapse
 
klawingco profile image
Marc Lawingco

I don't understand,
LINQ is much more readable than for each in anyday.
While foreach is faster than LINQ, its not that noticeable enough.

So the idea is
You want readability over performance = LINQ
You want a slightly faster performance over readiblity = For Each.

Collapse
 
ucavalcante profile image
Ulisses Cavalcante

Totally agree, ty.

Collapse
 
gruenwaldt profile image
Michael Grünwaldt

Hello,
since you asked for opinions.

For me the linq statement Is much easier to understand. Clear structure and short.

When one is used to linq i don‘t know how to write it clearer.
Even when not used to linq it is almost a sentence.

For me, in this case definately pro linq!
Regards
Michael

Collapse
 
ucavalcante profile image
Ulisses Cavalcante

Ty for your opinion Michael.

Collapse
 
ucavalcante profile image
Ulisses Cavalcante

Here an great article about.

link