[C#] Delegates and Events (E01: delegates)
HTML-код
- Опубликовано: 25 авг 2024
- In this two-part series we'll be looking at delegates and events.
This episode covers how delegates can be used to pass methods as arguments into other methods. It also very briefly touches on lambda expressions.
The next episode can be found here:
• [C#] Delegates and Eve...
If you'd like to translate this video, you can do so here: www.youtube.com...
If you'd like to support these videos, you can make a recurring monthly donation (cancellable at any time) through Patreon: / sebastianlague
Or a once-off donation through PayPal:
bit.ly/SupportG...
6:50 Magic happening :D.
Love when people explain by simply showing how it works, how we do without, and it's much better with.
This way you not only understand quicker, but you actually want to use the feature.
Excellent video.
this is the quickest yet most understandable tutorial i've found
the same @sebastian is the beast!!!
No it is not. What IS a delegate?
Nobody knows after watching this video. Useless.
@@bobsagget823 I had a hard time understanding it, but I eventually did. He teaches really fast at times, so the more experience you have, the better. But yeah if I were a beginner this would fly far above my head.
Would have been better an example with an event system. For example, when you click a button on UI, you turn all gameobjects to a random color which are subsribed to the event.
in other words, delegates are simply reference variables. They hold any number of methods as a reference (as long as the signatures match)
thanks bro
For those who are familiar with C, creating a delegate type is the same as creating a typedef for a function pointer. When declaring a delegate, you essentially declare a pointer of that type and assign the function address to it. When invoking a delegate, you indirectly call the function using the pointer. This of course lets you create arrays of delegates making for fast decoding and program flow. You can also have delegates "pointing" to array of delegates making for even more complex decoding.
Thank you
The only good video about delegates I found on YT. Everyone else is too busy gibbering the way they understood this concept instead of actually explaining it with clear examples. Good job.
I found a fault in your video.
You didn't introduce yourself with a 'HEEEEEYYY WHAT'S UP GUYS, THIS IS SEB, MY PATREON SUBCRIBE LIKES' etc
THANK GOD. Was tempted and decided ya know what...the 50 ish minutes he saved me by explaining clearly in 7 minutes instead of an hour with no HEY! WHAT"S UP hyper stuff is worth $5 right there. One time patreon x => x+5
I'm still trying to wrap my head around delegates, but I think you've explained them enough for me to try on my own. Thanks again for a wonderful tutorial, Sebastian!!!
Teaching is a talent. thats why Sebastian's videos are so wonderful. he is a great teacher.
It was the best explanation on delegates that i have seen
I have to say. I probably watched 10 or 15 tutorials about delegates and events in c#. This is by far the most effective and clear one. I think that most the coding RUclipsrs don't understand that when we want to learn something new, especially in programming where are so many layers of context, we want to learn in the simple way possible. And what I see most of the time, is the content creator explaining the thing that we want to learn attached to 20/30 other things that are not needed to explain this new thing that we want to learn. First, we ONLY want to learn the new tool, ONLY after that, we can try to apply that new tool to new contexts...So, Thank you for making a good example of what a tutorial must be. Congrats
Your two videos for Delegates and Events are the absolute best explanation in the internet. Thank you for this.
THANKS GOD other tutorials are like 30 mins boring and complex examples, but this one is amazing!!! Thank you so much!!!!!!!!!!!
THIS IS THE BEST VIDEO ON YT I HAVE EVER SEEN EXPLINING DELEGATES, Thank you learning this part was relly frustrating
Wow. Some people are gifted at taking troublesome topics and explaining them such that they seem simple. Thank you and very well done.
Thank you very much, I've been programming for almost 3 years now but I never understood delegates. You explained it so clear that finally i learned what delegates are :D
I really liked the examples. Straightforward, excellent explanation. Thank you.
Very down to earth explanation, wish I had seen something like this back in my freshman days.
Delegates are a way to parameterize behaviour instead of just values. The way you used it here is perfect example of a functor in real life. C# was a pioneer in implementing these kind of patterns in the language by avoiding a plain old pointer while at the same time actually using one behind the scenes
Awesome. A nice and clear way to explain why and how I should use delegates. Always wondered about them.
How the hell do I subscribe twice man I actually understand delegates now after scouring the entire internet for a half decent explanation
This is one of the best delegate tutorial i have ever seen. Please keep more detailed tutorials about programmming. You ara awasome and thank you!
I've been finding C# tutorial about delegates for so long
Thx God, I found yours.
After watching this, Delegates seems very easy.
jesus christ. so many years of not knowing this. thankyou Sebastian
Your videos are extremely professional and clean. The only problem I have is that you are so concise about what you say, that if I blink for a second a miss an important bit of information. Maybe you could take it just a tad slower. My brain would be very thankful. Great channel, already subscribed!
Best Delegate video I’ve seen. Nice job.
Your tutorial put everything together for me and everything just 'clicked' cheers..Thank you, Sebastian much appreciated!
I really liked this format for explaining concepts in C#. Top quality stuff
where was this 2 semesters ago, nicely done as always
Thank you, i've watched a lot of videos but this is the one that got to me. Simple how it works and why should you use it . Thank you .
Beautiful, thanks for taking the time to make this great video
Thank you so much for this video!
I never thought about using delegates like that to be honest, i used to just use them in the most basic way to call multiple functions at once, always simply thought of them as event broadcasters. This combined with the lambda expression is going to make some of my code a lot cleaner and less boiler platey, thanks again! :3
Thank you Sebastian! Another excellent tutorial. Looking forward to the part two. And really looking forward to a bigger more in depth tutorial on lambda expressions!
This is the third time I watched the video and I still couldn't follow, I'll watch several more times but I think I should read more document about it before pushing this thing into my head. I just have to learn this because it's needed for my game, it make life easier this way too.
Exactly how I feel! It is a good example but just isn't clicking.
this man knows how to explain
Definitely the best explanation of delegates i ever seen
clean and simple... bravo!!! Thank you very much!
It's pretty cool, Lambda expressions are useful ,but variable initialization should be considered when passing functions because not only do you have to worry what's being put on the stack ,but the reinitialized variables can cause latency issues unless dealt with properly. Of course the instance you showed was perfectly fine because that isn't repeating unnecessary code . Good Job Sebastian!! I have high hopes for this mini-series!!
Huh! This a very interesting different way to approach this,you are essentially doing Parameter injection on the function. Giving it the implementation of scoreCalculator you really want when calling it.
I love this video so much. This channel is a real gem.
You are my hero, love your heap explanation from another video
First time I've ever fully grasped lambda expressions
That's very helpful!
I had some trouble comprehending some of these concepts, but that clears things a lot.
Wow, your tutorials are excellent learning tools. Thankyou for making these videos.
Thanks for sharing, had no idea you could do something like this. Might take a while to get my head around before I start using it naturally in practice but it seems helpful :)
Also you might want to name this episode Delegates, then the next episode Events. Like having it as 2 separate episodes rather than a short series? Just an idea :) Cheers.
Hi, glad you found it helpful!
I chose to make this a 2-part series because it makes a lot of sense to learn about delegates and events at the same time (given that events are simply delegates with a few restrictions). I've edited the title to clarify that this episode only covers delegates though. Cheers :)
Sebastian Lague ah right that makes sense then, I had no idea what an Event was so I just imagined it was something completely different :)
OH MY GOD! I understood delegates in under 7 minutes!
Remember me ? Good news! I successfully understand this thing and E02: events. About a week huh ? Well at least it's paid off :D
Excellent Tutorial
Be blessed man.
thank you so much! noob as i can learn a lot from this.
Really good example, my code finally works!
Thank you so much!!! I finally get it!
I'm always a bit confused about the differences between Lambda expressions, delegates and events (the things of the type "Action")
But still I use Events for my Attack class. Each Attack has an impact-, hit- and activation-Event that is called within Invoke() of the Attack class. When the attack impacts on anything, hits a mortal-target or is activated it publishes the corresponding event with parameters like impactPoint, caster, hitObject etc... . The gain in this is that when i define an attack, i can put whatever function i like into those events or nothing at all. Therefore my utility-attacks use the same class and therefore Invoke as my spell-attacks, the only difference is that when i create the utility-attack i supply an activation effect only (e.g. instantiate some arbitrary game object like a lava patch) while on a normal attack i might define only a hit or impact effect.
Without that i would have to create a new function for every single effect my spells might exert. I would have to have a specific function that creates a lava-patch within the Attack class but now i just write "spellLavaPatch.impactEffect (dir, caster, pos) => { CreateSpellObjectbyPrefab(lavaPatch, pos); }" when i define the attack and the creation is neatly included when i call "spellLavaPatch.Invoke(dir, caster);" anywhere
At least that's what i do as of now, for me it is the first time utilizing those things.
Really good. Thanks
Amazing tutorial. I want to go over some of my old code and rewrite it now
Awesome, can't wait for the next one!
I would love to see some tutorials on Editor Scrips in the future. Keep it up!
Nice explanation.
Great video and a good refresher from some stuff I learned in school. Keep it up.
You're the best, keep posting please!
finally excellent use case for delegate usage
Where did the "stats" come from?
Thank you for the help! This was very great and is going to help me pass my c# test!
Delegates is implemention of Delphi 'function of object' (well, it's Anders Hejlsberg after all...). It's kinda logical as using of pointers was declared 'unsafe' but programmers still need to get an access to methods.
Thanks, dude.
Best explanation for delegates...and yet I stil need time to digest this :D
Really good stuff, as always
Events are great, but in some cases standard method calling can be more suitable - especially if you're making a strictly turnbased tactical game with an AI Sequence for example. I personally found that being able to see the methods being explicitly called within a complex algorithm (spread over several scripts) made it far, far easier to debug and analyse. I could pinpoint where I was going wrong much more quickly.
Events are a no-brainer in free-flowing games though, where you have multiple instances of objects interacting with other objects on the fly (in a simple way). But it's worth knowing that it can obscure your code somewhat on bigger projects.
Pretty much everything in Unity/C# has it's strengths and weaknesses, and events are no different.
i gotta say, delegates are probably the most useful thing there is in c#. Being a javascript guy, i always hate to take the procedural approach in problems where map, filter, reduce and higher order functions can be used. But i have to say, the type safety introduces a lot of verbosity D:
thanks for these videos man! Very helpful.
Never knew about these thanks for the lesson!
I am new to all of this, so do delegates optimize the code making it less redundant?
I don't understand this video that well, how do ScoreByKillCount and ScoreByFlagCaptures know about the delegate?
Excelent video!. My question is: why would I use a delegate instead of calling the function 'GetPlayerNameTopScore' twice with different arguments?
Thanks, you made my life easier again :D
thank you sebastian
Codes are become more and more intriguing as if some extraterrestrial are developing ✍️
I believe I have encountered a problem with delegates. They only work if there is only ONE instance of the object. The other ones will do what the first instace does.
nicely explained!
Great video!
I just don't understand the part where he passes in scoreCalculator at 5:30, can someone please explain? There is no function that's defined, so what's happening?
I know this is 4 months old, but scoreCalculator is just the name for the delegate and an integer is what gets passed in because at the top of the code is the delegate with the integer type. Hope I explained this well enough for you
Thanks for english sub-title. :)
Beautiful.
Would it make sense to compare delegates in C# too callback functions in JS/TS? Are there any important discrepancies between the two?
You might want to rename this episode to *just* delegates, since that is the only thing that you covered in this episode :P
the way i see it is that the only two uses for delegates are to make the input or output of a function be itself a function,(an example of which is shown in this video), and using them as events.
Is there an example of a situation where it would be appropriate to call an event instead of calling all the functions i know to be listening to the event directly?
cool, make me understand delegate deeper
sounds very similar to passing the reference of a function
Sorry, but can You explain in depht what it is "stored" inside the myDelegate(50) at timestamp 02:36 line 11?
what is the color pattern you are using for you code in this video, i really like it!
do more stuff like this....Thanks :)
actually we can assume than a delegate is a kind of class or in use of class at the same time
Amazing! Thanks :)
WoW! Grate video!
What happend to lambdas? Cant see a video about them
masterpiece
Hi. I would really appreciate a tutorial on how to do this in unity.
Hi, nice tutorials as always. I was wondering if you actually have finished a complete game?
Could you tell me the theme and font you use for your editor? It looks really nice.
But steel is heavier then feathers? I don't get it.
Suple, super thank you
So, in short, delegates are like variables for storing and invoking methods?
Int x = "Hello world!" // Doesn't work
delegate void DelegateTest();
string StringReturnMethod(){
return "Hello world!"
}
DelegateTest myDelgate = StringReturnMethod; // Also doesn't work
Fascinating... ^^
I'm having trouble understanding the foreach statement.. why is 'stats' allowed when it was not declared.?? is it because of the 2nd parameter scorecalculator??
@3:45 stats is being declared while in use