Monster shadow


Lambda expressions are awesome - C# tutorial

Lambda expressions can reduce your code, make it more readable and turn you into a more efficient programmer. I'll give you the rundown on what lambda is, how it's built and show you a bunch of useful lambda functions. Lastly I'll show you how to create your own lambda functions. Give the video a like if you enjoyed it! Also, come join the discord:

Icey Leung
Great video! Really glad to see tutorials that cover more advanced topics for beginners. I do find this tutorial covers a lot of System.Linq, which might cause beginners to mix that up with lambda expressions. Lambda expressions is simply a short hand to create an anonymous function. I think it might be beneficial to cover the following: - Differences between anonymous functions and Linq/Extension methods. - Other usage of anonymous functions (i.e. assigning a function to a variable/parameters, callback using System.Action)
Really nice tutorial. The only thing I disliked is that you used var so often. To understand the connections a little better the specific datatypes would have been better for me.
Great explanation
jos bexerra
Gracias Mister Tarodev que poder de las expresiones Lambda.....saludos de los andes peruanos
Evar Dion
there is a downside to using LINQ lambdas instead of for each and that is you can't step through the code that is being iterated upon, so while it may be simpler to write, you are actually making the code much harder to debug. This is fine if the code is simple and will not throw any exceptions but as soon as the code becomes complex and is likely to throw exceptions while iterating through the list then you'll have a really hard time debugging it.. An example of this is using LINQ to initialize a list of objects from a set of records that come from a database. Lets say there is a conversion error when attempting to convert one of the values from the DB into the datatype of the object property you are trying to set. Using LINQ you have no easy way of knowing which item in the list failed. So yeah lambas are great until there is a problem that can only be debugged by stepping through code.
@R1PFake I actually did not know this. Thanks for the tip!
Which editor are you using? With full Visual Studio (not sure about VS Code) you can debug lambdas and place breakpoint inside lambdas (important, the cursor must be inside the lambda before you add the breakpoint, otherwise it will be added at the wrong position) then you step through the code and debug it like a normal loop iteration, where you would place the breakpoint inside the loop code.
You're right. Writing actual loops also gives you more granular control, thus if done right, better performance. This is negligible for 99% of cases, but it should be known. Regarding your point, it helps having tooling which can convert lambdas into loops and back, such as the power hungry resharper. Side note: Sometimes I convert some nested loops into lambda using resharper and the resulting lambda gives me a brain haemorrhage.
One Word: Closures
@BEYOND HELP I'll close you in a minute sir
Have a good day everyone...
oh boy 2:40 hit way to hard. great vid
Sean N.
You sir, are a legend. Thank you for this.
This is more like a System.Linq tutorial than general lambda expressions. But anyways this is still good content.
This actually was so simple but helped so much, I hope you can do a “C# tutorial series” or something along that line! Keep up the good work man
Nice video! Can you share your thoughts on using LINQ in Unity? Anything we should keep in mind?
@Tarodev Thanks for the thoughts! Appreciate it
Linq makes code clear and concise, but can sometimes be less performant than a standard for loop. But understand this can be the difference between it running 100,000 times a second instead of 105,000 times... so don't worry about performance until you have to because 99% of the time it will be fine.
Mike Lambert
Hey man, i have to say it, your explanation was crystal clear. It was as relaxing (no noisy music and what's up boyz) as it was helpful. Thank you.
Although it still has a pretty stupid intro 😜
This was the first thing I read after waking up. Thanks mate, glad you enjoyed it.
JohnnyBe Cat
I like this video because you encourage people to look into other options. :) Keep up the good work!
Albert Stone
Awesome video! but.. feedback is weak, few comments.. As for me, you need a target audience. Without help, it will take a long time, but you can try u t i f y OR maybe u t u b e r for real views!
Wow, C# is really great, thanks for these infos
Константин Лактионов
It was very hard for me, I don't understand this. But I guess it's cool.
These are really nice videos. Question - 2:09 couldn't you pretty much do the same with list.FindAll? I usually use that, then there is no need to use Linq.
@322ss It comes heavily into play when you start using a db ORM. You can construct a linq query to grab data from the db, but not actually process it until it's ready (you may actually skip processing completely depending on logic). As soon as you try do something with the IEnumerable it activates. So if you know you'll need to access the IEnumerable more than once it's always better Listing it to avoid double handling. But honestly, in 99% of cases we're talking such minor performance deltas its not even worth thinking about.
@Tarodev Yep. I have to admit I have never (so far) found any reason to use IEnumerables... maybe I should, but lists and arrays have been more than enough for me, I have not seen any reason to use something more generic, and for some reason I have not seen any need to use Linq either... maybe I should learn more about both of these.
They are both situational and should both be used. The beautiful thing about 'Where' is it returns an IEnumerable and can be used on an IEnumerable. FindAll is a List extention so it can only be used on lists. Plus it returns a new List, which may be a good or bad thing.
Isobel Shasha
cool vid not a boy tho
You're part of my 1% female demographic! Thanks for watching
Isobel Shasha
thank you for braking down lambdas in a way that might actually stick in my head for once haha
Koray Deger
As a fellow game developer, I humbly suggest your tutorials to my team, especially the simplicity and real world samples are just on point. Keep up the good work and thank you for sharing👍
They'll sit down to the intro of the video and think what the hell is this
That comment made my night mate. Thank you!


if you boys are not using lambda yet well you need
to focus on me because i'm just going to introduce you
to a wonderful world so come on let's go say we have a list of integers and we'd
like to create a new list of all the numbers which are over five
traditionally you would do something like this
but with lambdas you can do the same thing but with much less code
let's break it down we begin by referencing the list
we'd like to filter then we call a function
which is suitable to the situation in this case i'd like to grab a subset from
this list so i'll use the where function we're
saying give me the items from this list where
my condition is met in the parentheses we start by supplying
the argument to the function this can be looked at the same as an
argument given to a normal function if it's more your style you can give
your lambda variables more descriptive names
but this is entirely up to you i much prefer short concise names
following is the arrow function or more suitably known here as the lambda
operator this allows you to create an anonymous
function which is just an inline function without a name
the variable and lambda operator is the equivalent
to this section of a real function last comes the actual function
logic the where clause expects a boolean value
to determine if this item in the list should be included in the new list
this section is equivalent to the inner logic of a normal function
so to summarize the where clause will run this section of code
for each item in the list and return the current item if it's greater than five
let's explore some additional lambda methods here
we have a list of steam games let's say we'd like to know if all the games have
a rating of at least 9 and above for this we can use the all function as
you can see it follows the same syntactical
conventions the where function does here we're asking if all the items
satisfy our condition to which the answer is no cyberpunk
destroyed our condition just like it destroyed our hopes and
dreams without lambda this code might look something like this
okay what if you'd like to grab a list of the names
we can use the select function to do just that
by the way innumerable is a collection interface
it allows us to establish the idea for a list
without actually processing it only when you call on it
does it get computed lazy loading if you will
we can force it to process immediately by using the
to list function select is very powerful as it allows you to create entirely new
objects let's say we have another object called
star rating which has the name of the game and a
star rating out of five we can take our game list and return a
new star rating object using the properties of the original
item we'll divide our rating by 2 as the max star rating is 5 not 10.
another common function is first this will find the first item which satisfies
the condition and return it but be careful this will
throw an invalid operation exception if the list
contains no elements which match an alternative strategy is to use first
or default this will return an item if it finds one
otherwise the variable will be null okay let's end with something
interesting let's say we work for steam and we need
to recommend three games to the user on the home screen
the games need to fulfill the following criteria
they need to have at least a score of nine they need to be released
after the year 2018 and the games must be randomly selected
so that the user gets a different set of games each time he visits the homepage
i've added some additional games to make it a bit more interesting
first let's assemble a list of games which satisfies our demands
just as a traditional if statement we can use the and operator to check for
multiple conditions the beautiful thing about lambda
expressions is that we can string together multiple functions with ease
so now that we have our filtered games let's shuffle them to ensure we're
presenting a random set to the user we can use the order by function to do
just this whatever value we return from this
function is what will be used to order the items
for example we could place the game score here
or the release date but we want to randomize this list
so let's use the random class to just return a random value
now the list is shuffled we need to grab three values from the list
we can use the take function to do this take will grab the first three items
from the list which is fine for us as it's all been
shuffled around pretty freaking snazzy right one last
thing i'd like to show you is how to create your own lambda
function i urge you to take a look into the many
wonderful functions available to you what i've shown you today is just a tiny
glimpse of its offerings