Тёмный

LINQ + Lambda expressions - C# tutorial 

Tarodev
Подписаться 94 тыс.
Просмотров 104 тыс.
50% 1

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: / discord
❤️ Become a Tarobro on Patreon: / tarodev
=========
🔔 SUBSCRIBE: bit.ly/3eqG1Z6
🗨️ DISCORD: / discord
✅ MORE TUTORIALS: / tarodev

Опубликовано:

 

18 июл 2021

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 201   
@jacobs.7925
@jacobs.7925 Год назад
My only wish is that this video was 35 minutes long with 20+ examples instead. So great!! Thanks a ton!!
@DannyBoy443
@DannyBoy443 Год назад
Yeah he and I have talked about the speed of the videos too. I'm not a beginner to C#, but I'm not a pro yet either.
@aj.arunkumar
@aj.arunkumar День назад
search "101 linq examples" in google
@kdeger
@kdeger 3 года назад
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👍
@Tarodev
@Tarodev 3 года назад
That comment made my night mate. Thank you!
@Tarodev
@Tarodev 3 года назад
They'll sit down to the intro of the video and think what the hell is this
@SlimeWithClass
@SlimeWithClass 3 года назад
This tutorial was perfect, I really like how you compared lambdas to a more simplistic loop. It helped it click in my head
@Tarodev
@Tarodev 3 года назад
I was hoping it would serve that purpose! 😊
@koferperk
@koferperk 2 года назад
i agree
@CyCloNeReactorCore
@CyCloNeReactorCore 2 года назад
Except, this is really the exact opposite of what you were intended to get from the video. The lambda is not the loop. LINQ is running the loop based off of the condition from the lambda expression, to my understanding. Although, I'm nowhere near tarodevs level of knowledge, and I could just straight up be wrong lol.
@justiceokechukwu7728
@justiceokechukwu7728 Год назад
@@CyCloNeReactorCore You're right mate
@samadhistate9637
@samadhistate9637 2 года назад
You break things down and explain it really well. Also your voice is clear, calm and not rushed. And no stupid drawn-out intros. Thanks for posting - will watch more of your tutorials.
@_acea4171
@_acea4171 2 года назад
THANK YOU SO MUCH. I've been looking everywhere for a tutorial which really deconstructed lambda expressions, but couldn't find one anywhere. You are a lifesaver.
@eerongal
@eerongal 2 года назад
While i agree that lambda's are awesome, what you're mostly focusing on here is LINQ, and NOT LAMBDA's (though you are using lambda's with linq, which isnt not really required). The select/where/orderby/etc are all LINQ functions. The "n => n>5" is the lambda part. Everything else is LINQ. Lambda's have FAAAAR more uses than just LINQ. And LINQ can be used without lambda's.
@Tarodev
@Tarodev 2 года назад
To be fair, I call the arrow function a lambda operator and make sure there's lambda in every example. I debated including the distinction between the two and decided against it. It's an inner battle deciding what to include and cull from each tutorial and there's usually more thought that goes into it than what most people think. For example, would it confuse people unnecessarily while they're trying to process a concept? Re-read your comment and tell me (although written well) it wouldn't be confusing for a brand new dev.
@CyCloNeReactorCore
@CyCloNeReactorCore 2 года назад
​@@Tarodev As a fairly new dev, watching your video, I understood the lambda to be the method chaining, and the lambda expression within the parenthesis was also called lambda. This led to alot of confusion when I found out that wasn't the case. IE "why does his lambda look nothing like mine?" in reference to the LINQ aspect. Rather than "Lambda (+LINQ)" it really just feels like "LINQ (+Lambda)" That being said, this video was very helpful, and did lead me into a grasp on Lambda expressions and statements, so the video itself wasn't bad at all, I would just say to re write the title, so it isn't as misleading.
@Tarodev
@Tarodev 2 года назад
@@CyCloNeReactorCore Yup, rewatching it I did a poor job of distinguishing them (or even mentioning it >
@RayanMADAO
@RayanMADAO Год назад
@@Tarodev It's okay!! Not every video can address every single facet of the subject. You helped me understand lambdas more so thank you.
@daruma5018
@daruma5018 Год назад
@@Tarodev "If I Had More Time, I Would Have Written a Shorter Letter" - Blaise Pascal
@Worlnof
@Worlnof Год назад
That Cyberpunk joke cracked me xD
@raoulbest5395
@raoulbest5395 3 года назад
This is extremly helpful. I've known a bit about lambda functions before but making one actually blew my head off.
@Tarodev
@Tarodev 3 года назад
They truly are amazing. I rarely find myself writing loops nowadays... In both game dev and my professional day job.
@activate9741
@activate9741 2 года назад
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
@bernardodelcast
@bernardodelcast 2 года назад
I have struggled with lambdas A LOT (I think mainly because most examples of it that I had seen were so abstract and high level), but this has been probably the best explanation of them yet. Thanks!
@faremir
@faremir 2 года назад
There's also the thing about performance. Althought in many case LINQ is fasterish and less bug prone than manual foreach ( things like .Except() and especially parallel aggregation), most of the time LINQ will be slower, because of the overhead of delegate invocation. And not even speaking about for loop which is way faster then foreach. So if you actually need to care about performance for example rendering, real time data analysis, etc. it's good to be aware and benchamarking of test subset won't ever harm you.
@matthewiskra771
@matthewiskra771 2 года назад
And as an Olde Timey Programmer (kids... I did key-to-tape! I read EBCDIC, UNICODE, and ASCII! Y'all don't know the pain of dropping yer deck and doing a floor sort! Git off my iLawn before I sic my eDog on ya!): Always design and code for performance from day one if you ever expect to have high performing code. That being said, this is cool, and better yet, useful.
@LuukDomhof
@LuukDomhof 2 года назад
There are MANY cases where a foreach loop is faster than a for loop. Check some benchmarks ;)
@faremir
@faremir 2 года назад
@@LuukDomhof In any case my previous comment was specifically targeting LINQ. Either way... I agree there are cases (even tho highly specific) where foreach is slightly more efficient than for loop. And even with Ben Watson's great article back in 2014 (.NET loop performance) there is no general rule. That's why I suggested that people should make their own tests and know what their code is doing.
@paulkohler8868
@paulkohler8868 2 года назад
I enjoy your videos. Though I've been using and making lambda functions for some time, seeing them this way taught me something new.
@jnicoulakos
@jnicoulakos 2 года назад
I like this video because you encourage people to look into other options. :) Keep up the good work!
@windup247
@windup247 Год назад
You could honestly teach teachers how to teach. You're that good.
@Icey0812
@Icey0812 2 года назад
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)
@CyCloNeReactorCore
@CyCloNeReactorCore 2 года назад
Exactly! This is what happened to me for a brief while when i first watched this video.. Great video though!
@gregorydeclercq2990
@gregorydeclercq2990 Год назад
The world of lambda expressions seemed so harsh and difficult, until I saw your video. Thanks for the simplicity 🙏
@castlecodersltd
@castlecodersltd Год назад
Great video. Always good to refresh your knowledge or learn new stuff
@mikelambert5426
@mikelambert5426 2 года назад
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.
@Tarodev
@Tarodev 2 года назад
This was the first thing I read after waking up. Thanks mate, glad you enjoyed it.
@Tarodev
@Tarodev 2 года назад
Although it still has a pretty stupid intro 😜
@TheOriginalDarkGlitch
@TheOriginalDarkGlitch 2 года назад
RU-vid has been recommending this video for literally half a week. I should have watched this sooner lol. Great video!
@Tarodev
@Tarodev 2 года назад
Sometimes they know, lol
@blerd7068
@blerd7068 2 года назад
Understood within 2 minutes of the video. Great Job. I need to now practice. Thanks!
@kellybmackenzie
@kellybmackenzie Год назад
This is amazing, thank you so much!!
@reggieisnotadog4841
@reggieisnotadog4841 9 месяцев назад
I have never understood lambda functions before and you just made it so easy. Thank you so much for this video, even if I do feel a bit sick at all the time I wasted writing loops that I didn't have to write.
@Tarodev
@Tarodev 9 месяцев назад
Just don't use them in update 😊
@rambertoeco8930
@rambertoeco8930 2 года назад
Man, you never disappoint, thanks!
@omkargodse5726
@omkargodse5726 9 месяцев назад
Wonderful! So easy to understand!
@schmidtlach
@schmidtlach 5 месяцев назад
👏👏👏Excellent explanation. Clear and to the point.
@Vykhari1
@Vykhari1 Год назад
Thank you very much. It's really helped me!!
@Notreal76
@Notreal76 Год назад
You are a great Teacher. Thank you
@jugibur2117
@jugibur2117 2 года назад
Wow, C# is really great, thanks for these infos
@AyItsKevin
@AyItsKevin 2 года назад
Love the threatening aura intro then complete coddling immediately after 🤣 needs to be in more vids
@Tarodev
@Tarodev 2 года назад
K & d in the wild
@Tarodev
@Tarodev 2 года назад
K & d in the wild
@goverdhanjayaram3683
@goverdhanjayaram3683 8 месяцев назад
Thank you very much ! Very useful video !
@divandebruin5767
@divandebruin5767 Год назад
Thanks very much for this, subscribed long ago 🎉
@denisstasyuk6738
@denisstasyuk6738 2 года назад
Just to add to all comments above, your typing speed is outstanding😳
@datbio7302
@datbio7302 10 дней назад
very clear and easy to understand!!
@kingreinhold9905
@kingreinhold9905 2 года назад
Dude you really have a special gift in teaching complicated topics... There's really no better way of breaking down this topic... Thanks for your good work!
@abhisheksuper20
@abhisheksuper20 Год назад
Just purely amazing!!
@fiscpar2305
@fiscpar2305 Год назад
Jacked and C# instructor? I am among my peeps! Thank you for your precious help!!
@synchaoz
@synchaoz 2 года назад
You sir, are a legend. Thank you for this.
@Kukuthepooch
@Kukuthepooch 2 года назад
The cyberpunk joke won me over. Good job.
@luigidreemurr6034
@luigidreemurr6034 Год назад
That burn on cyber punk though. God damn! Very informative. I know how to use lamda but not really why it worked that way. Even picked up some new uses. Thank you for a great video!
@Ne1gh_
@Ne1gh_ 5 месяцев назад
the best tutorial i've ever seen
@torrvic1156
@torrvic1156 10 месяцев назад
That was rad dude!
@setpopa5357
@setpopa5357 Год назад
Man this was great just explained two advanced topics in 6 mins
@erandafernando94
@erandafernando94 2 года назад
thanks fam! great video!
@lukamuller7305
@lukamuller7305 Год назад
Great Tutorial Thanks m8
@shariel9731
@shariel9731 2 года назад
This was really helpful, great tutorial!
@Tarodev
@Tarodev 2 года назад
Thanks mate 🙏
@tonykamin3840
@tonykamin3840 11 месяцев назад
Nice tutorial, and bonus points for including Valheim in the list of Steam games (one of my all time favorites). 😀
@IvarDaigon
@IvarDaigon 2 года назад
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.
@Tarodev
@Tarodev 2 года назад
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.
@r1pfake521
@r1pfake521 2 года назад
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.
@Tarodev
@Tarodev 2 года назад
@@r1pfake521 I actually did not know this. Thanks for the tip!
@scevvin7788
@scevvin7788 Год назад
Love the golden ratio used in the list example
@sudeepjainsudeep
@sudeepjainsudeep 2 года назад
Those have a C# background before moving to Unity like me are already using it extensively. During the course I have read on some blogs and Q&A forums that Unity might have performance difficulty while processing these expression. Some even said Foreach loops are bad. But so far I haven't observed any issues. Go for it, you can reduce your code by 30% and less time on maintenance.
@NyteShade76
@NyteShade76 2 года назад
I don't think that Cyberpunk reference will ever get old, even if Unity updates their code 10 years from now :D
@THVLPLYR
@THVLPLYR Год назад
the intro has this vibe i cannot explain....
@bquimby5223
@bquimby5223 Год назад
Whoa. Solid video
@nested9301
@nested9301 2 года назад
I'm in love with c# wow
@NotNazuh
@NotNazuh 2 года назад
Great explanation
@HemmingEducation
@HemmingEducation Год назад
Awesome!
@henrik3098
@henrik3098 Год назад
very good thank you my friend
@spirit5923
@spirit5923 Год назад
This stuff was so much fun to play with. Proceeded to write the dumbest code to make a string basically nonsensical and I couldn't be more proud of my work.
@Tarodev
@Tarodev Год назад
I'm also proud of you
@spirit5923
@spirit5923 Год назад
@@Tarodev Thanks, papa!
@gustavosalmeron2013
@gustavosalmeron2013 Год назад
Your videos are AWESOME! You are very didactic, patient and interesting, thank you so much! I've been trying to learn pure C# but whenever I ask questions to a "friend" of mine, he mocks me, says I should have known that already, that my code looks like shit.... You, on the other side, make the learning process productive and healthy. Thank you so much!
@dnkywnky
@dnkywnky Год назад
You should probably get a new friend
@torrvic1156
@torrvic1156 10 месяцев назад
He is abuser. You should define your borders in your relationship and tell him what you like and what not. Just talk with him about your feelings.
@gustavosalmeron2013
@gustavosalmeron2013 10 месяцев назад
@@torrvic1156 actually, I don't speak with him anymore. I blocked him and don't answer him, I don't even have contact. Never been happier!
@CoffeeKavat
@CoffeeKavat 5 месяцев назад
best unity tutorial!
@thatdotadev
@thatdotadev 2 года назад
If only this video would last for an hour showing examples. Amazing stuff
@chadbaldwin652
@chadbaldwin652 2 года назад
OMG I FINALLY GET IT. THANK YOU. lol
@fulongfromthegrave
@fulongfromthegrave 2 года назад
underrated!
@fredimachadonet
@fredimachadonet 2 года назад
Awesome! Thanks for your content! Not sure if someone already mentioned this before, but as soon as you use foreach in a lazy IEnumerable it will force the query execution. Not sure if that was the intention in your AddRatingToNames method. If you want to stay lazy you could do this instead of using the foreach: return games.Select(g => { g.Name = ...; return g; });
@TroL0iO1er
@TroL0iO1er 2 года назад
This is more like a System.Linq tutorial than general lambda expressions. But anyways this is still good content.
@betterlifeexe4378
@betterlifeexe4378 2 года назад
Been using linq for a while now. Love the lazy processing and REAL composition, not just employing a tool for composition, which is what the ':' really is for c#. there are more rules you have to follow for real composition, but it gives you so much. Which is one of the fundamental reasons why linq is AMAZEBALLZ. I would not be surprised in the least if the first intelligent aliens we meet have something startlingly like linq on the IL level. It is kind of close to a pure logical expression of the act of parsing related data. Maybe we should be pumping low level linq code into space, prove that there's at least some intelligent life stuck to this rock.
@ceroenblanco
@ceroenblanco Год назад
Thing I didn't know I needed to know
@josbexerra8115
@josbexerra8115 2 года назад
Gracias Mister Tarodev que poder de las expresiones Lambda.....saludos de los andes peruanos
@gustavo3220
@gustavo3220 Год назад
love your sense of humor
@Tarodev
@Tarodev Год назад
I love you
@gustavo3220
@gustavo3220 Год назад
😳
@pinguinpinguin-zv3fh
@pinguinpinguin-zv3fh 7 месяцев назад
make more of these you're an exam saviour....
@breakchiller
@breakchiller 2 года назад
Damn good bro
@kopilkaiser8991
@kopilkaiser8991 Год назад
I just feel calm looking at these tutorials and even on the way I'm getting smarter and developing my code knowledge
@sipepguru
@sipepguru 4 месяца назад
Subbed.. lovely
@kexell
@kexell 2 года назад
Good video
@castlecodersltd
@castlecodersltd Год назад
Great ☺
@neilgargan
@neilgargan Год назад
"Cyberpunk destroyed our condition, just like it destroyed our dreams" - Tarodev
@patrickp4175
@patrickp4175 2 года назад
damn, this is called lamda? so I did it on accident every time. Learned this on tutorials and didn't knew the other way thx for this :D
@kawaiianthony8090
@kawaiianthony8090 2 года назад
compact tutorial!
@taylor527
@taylor527 6 месяцев назад
I feel for you bro. I know that mood.
@Thiago1337
@Thiago1337 11 месяцев назад
bro is teaching and doing asmr at the same time
@Tarodev
@Tarodev 11 месяцев назад
😉
@spadigha.s4920
@spadigha.s4920 13 дней назад
Unexpected bonus for Extension methods at 05:39 !
@Tarodev
@Tarodev 13 дней назад
Use it wisely :)
@Nature_Listener
@Nature_Listener 2 года назад
Hi could you explain unity physics with examples ? Thanks great content btw
@anshumanyadav24816
@anshumanyadav24816 Год назад
Big Like ( within start of a min ) watching this video
@markroworth5700
@markroworth5700 2 года назад
"just like it destroyed our hopes and dreams..." - genius.
@TrailersYT
@TrailersYT 2 года назад
Hey, what if I wanted it to be based on luckPercentage? I have a dictionary of items, each item has a float luckPercentage Item 1 = 10.0 Item 2 = 40.0 Item 3 = 50.0 So if I would want to take 1 item, item 2 and 3 would be the most often picked, and item 1 would be very rare. How can I achieve something like this?
@tonytran07
@tonytran07 2 года назад
Question: If there is a list of items separated by two categories (owned and not owned by the player), should I use forloop or lambda? My thought is, if I use lambda, I'll have to do it t twice, thus I presume it loops through the collection twice, whereas running the loop just goes through once to separate the list. Your thoughts?
@Jacob___THE_Jacob
@Jacob___THE_Jacob 2 года назад
cyberpunk burn lol, hilarious hahaha :)
@Lonchanick
@Lonchanick Год назад
More examples, nothing else!
@GonziHere
@GonziHere 2 года назад
It's all fine and dandy, but hard to debug, which is why we don't use these as often. But maybe you have some debug tips?
@DerTimmey
@DerTimmey 2 года назад
oh boy 2:40 hit way to hard. great vid
@jkwong1978
@jkwong1978 3 месяца назад
can i change the value of the list using lambda ? like find the game named factorio, set its steam score to 9, using lambda, without creating a new list.
@Lonchanick
@Lonchanick Год назад
belleza!
@j0gi
@j0gi 2 года назад
2:40 yup, that's a like and sub right there
@jean-michel.houbre
@jean-michel.houbre 2 года назад
Hmm .. It's more of a tutorial on Linq, isn't it? But clear and useful!
@daswesen123
@daswesen123 2 года назад
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.
@DisturbedNeo
@DisturbedNeo 2 года назад
Huh, I don’t know why I’ve never thought to make extension methods for an IEnumerable to basically make my own LINQ functions. Cool idea.
@ewwitsantonio
@ewwitsantonio 2 года назад
Nice video! Can you share your thoughts on using LINQ in Unity? Anything we should keep in mind?
@Tarodev
@Tarodev 2 года назад
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.
@ewwitsantonio
@ewwitsantonio 2 года назад
@@Tarodev Thanks for the thoughts! Appreciate it
@NadjibBait
@NadjibBait 2 года назад
@@ewwitsantonio As a general rule, don't run LINQ in the game loop (Update method) for big collections/complex queries. But for initialization code (Start, Awake...) or code that does not run each frame, it's totally fine.
@NewLondonMarshall
@NewLondonMarshall 4 месяца назад
Always give variables descriptive names because it's not just going to be you reading it. Other than that great video thank you!
@deltekkie7646
@deltekkie7646 2 года назад
What am I missing??? When I do the same code you do I get an error. CS0246 C# The type or namespace name 'Game' could not be found (are you missing a using directive or an assembly reference?)
@ThunderaRafa433
@ThunderaRafa433 27 дней назад
you can use predicates lamda? right.
@dudds6699
@dudds6699 2 года назад
I not only use lambda's I use them all of the time with Joins and GroupJoins good times
@dudds6699
@dudds6699 2 года назад
Should have included a inner join and outer join IMO but venn diagrams in code might break your audience.
Далее