DEV Community

Discussion on: Using Extension Methods in C# to Build Fluent Code

Collapse
 
silviosavino1 profile image
Silvio Savino

Great article but what do you think about the allocations caused by LINQ?

As a game programmer I've been taught to avoid using LINQ, especially in scripts running in real-time on the main thread in order to avoid allocations and so sudden frame drops caused by the GC kicking in. Surely a game is not comparable to other types of programs and besides not every LINQ method necessarily allocates (I sincerely don't know). Maybe some of the allocations could be even avoided, like those caused by the Func passed by simply caching them, but then it would lose in readability.

It's a tool like many others .NET put you at your disposal but would you still suggest it for environments like games?

Collapse
 
integerman profile image
Matt Eland

I use LINQ by default. However, I did find while profiling a roguelike's core AI routines that LINQ significantly added to the duration of the application. However, this was a core routine in a genetic algorithm that had to run millions of times.

My suggestion would be to go with LINQ until your data tells you to avoid it in key places, then make those optimizations.