Use C# LINQ in your Unity projects to write code faster!
LINQ is one of my favorite features of C# (if you’re being finicky, it’s technically part of the .NET framework). LINQ is a lot of things, but I primarily see it as a set of extension methods that allow you to do common operations on collections very easily. For those of you who haven’t heard of LINQ, but have used C#, please read on! This stuff isn’t complicated and will save you a ton of time throughout your C# programming career adventure!
Why should I use LINQ and how do I use it?
First, let me show you how a simple programming task can be done much quicker (and cleaner!) with LINQ. In this example, we want to check if any of the enemies in our Unity game are alive. I’ll show you three ways we could do this:

That example above should be pretty clear. However, most of you should know about the foreach loop, which abstracts away the iteration process:

That example was better, but still several lines long! As programmers, we loop through collections all the time to do simple tasks- there must be a better way!
Well, just add the LINQ namespace to the file…

And then we can do this instead…

Wow, just one clear, clean line of code! But what is this Any method, and how is it related to LINQ?
Well, when you import the LINQ namespace, it imports a bunch of extension methods that operate on collections. This means that a bunch of extra methods are now available to call from any enumerable collection, such as lists, arrays, and sets.
These methods are incredibly useful. Below are a bunch of other examples of things that LINQ can do in one line of code, that usually take several lines to achieve. Don’t worry if you don’t understand the => symbol, I’ll explain it soon.

These methods are kind of special; instead of taking ints, floats, strings, or objects as parameters… they take functions! That’s not as complicated as it sounds (to beginner readers, you can consider methods and functions roughly the same thing). Let’s take a short diversion to talk about anonymous functions, in case you haven’t heard of them yet.
How do I use Anonymous Functions?
In C#, you can define an anonymous function (that is, a nameless ‘inline’ function / method) quickly on the spot by using the following syntax
x => { return x + 1; }
Basically, the above line defines a function that takes a variable x and returns x + 1, except it does so in a special short form syntax. Don’t worry about types, C# will magically figure them out for you!
If your anonymous function isn’t too complicated, you can drop the brackets and just write:
x => x + 1
So why are Anonymous Functions important for LINQ?
So, the LINQ extension methods usually take functions as parameters! These are usually small simple functions, so the anonymous function syntax allows you to pass a function as a parameter quickly and simply.
Take a look at this example below. Notice that the Any method expects an argument of type Func<Enemy, bool>. This just means that it expects a function that takes an enemy and returns a boolean.
The Any method will iterate through all the enemies in the collection, and call our anonymous function enemy => enemy.alive on each one. If this function returns true for any of the enemies, then the Any method also returns true.
By passing functions to LINQ extension methods, a lot of complex behavior can abstracted behind simple rules. Programmers deal will collections all the time, in Unity or elsewhere, so using LINQ can really help speed up your workflow. Check out the full list of LINQ functions here.
One word of warning about performance: although LINQ uses less lines of code, it does not run faster than for-loops or foreach-loops. You should be aware that LINQ runs similar for-loops inside it’s own methods. LINQ is shorter, more readable, more maintainable, but not faster. If performance is critical to your application, there are some situations where you might want to be careful about using LINQ. Check here to learn about these situations, but for many projects, these issues probably won’t become problematic. I’d argue that you should write readable / maintainable code first, and optimize it if necessary.
Thanks for reading. Please let me know if you have any questions or I can make this more clear! Happy Game Dev!
