C# Tutorial: Events/Event Handlers

preview_player
Показать описание
In this lesson I go over events/event handlers and how to use them.
Рекомендации по теме
Комментарии
Автор

Just a tip for anyone watching this as its a very simple guide to Events in C#, he actually missed a CRUCIAL point in this tutorial. you should always always check the EventHandler (ClickEvent in this case) for null with the Invoke. if you do not and nothing has subscribed to the event, it will crash the program. you can either do if (ClickEvent !- null) ClickEvent.Invoke() or shorthand it with ClickEvent?.Invoke() (the ? at the end is a shorthand version of only do this if its not null)

problemchild
Автор

Thank you so much, I have read through documentation and watched several videos, but this explanation and demonstration finally made me understand how to actually use them!

inspiration
Автор

You are my hero man i could finally understand this, really thank you

killermaker
Автор

Thanks for posting this. I'm not sure why you don't have more subs.

shaunhunterit
Автор

Awesome! Short and sweet! Very helpful!

hchattaway
Автор

This example is more relevant and understandable even more than the ones in the official content

christopherkaggwa
Автор

It's a hard topic. Best expression imo

omerask
Автор

Bro think for the video I finally learnt what I need !

Автор

I have a "button" class that I'm creating dynamically. Each button will report a different string to a handler when it has been hovered over. What I've been doing is giving my button class a reference to the parent, and then will call "ReportUp(str)", but I don't think this is a good design. Does this solution help with this or not really? I'm also using Unity but I don't think that matters

sheffieldk.
Автор

Ian brother do a fresh C# course, Your content is good this is helping me today! <3

codecakeee
Автор

Question: why is there a need to use the custom EventArgs when you can simply utilize the class where the EventHandler belongs? To be specific in this example: why create a new MyCustomArguments class when you can instead add the Name property in Button and then on the anonymous function just call s.Name instead of args.Name?

randlmorales
Автор

for people that still didn't get it,
1. you create a method like public void OnRick
2. if you are confused by the On before rick, it's just a naming convention
3. just like a method you create something called an "event" the difference here is you instead of void or any return type for that matter you type in EventHandler, wth is that?, it is a container ( something that holds something ) that holds in a method, People also named them DELEGATES, something that holds a method (don't go for the reference/pointer bs) I think most people got confused because he used an anonymous method up there (the line which contains this "=>")
4. if you do not know what invoke is or are still confused,  it's to  create a method, ....   name that method something  
[ex: somemethodname]    and call it by typing  somemethodname() {and calling means to execute},  in other words, to run the code inside that named method
5.  Now the container on itself cannot do anything so you say eventhandlername.invoke to call it
6.  Eventargs = parameters, why pass it that weird way, by saying eventargs, think about it
7.  In the keypressed method he stored the method he wanted to execute in the event handler named ClickEvent, then he called the onclick method

That's it

WaahaidIWKY
Автор

Where I am confused is... why was there no event here? only a delegate?

luisarias
Автор

Much appreciated. Just one issue I found, the console window closes immediately so I don't see the message. Does it additionally require a Console.ReadKey() after button.OnClick()?

zishthefish
Автор

i just came across this vid and i love the explanation but
can you explain why MYCUSTOMEARGUMENTS need to inherit EventArgs

olympustech
Автор

I don't quite see the difference between events and event handlers and boring old callbacks with custom delegate functions?

BastianInukChristensen
Автор

The event construct reminds me of the Observation Pattern.

robinsmit
Автор

But how do you create the button itself as an object?

REktSigMa
Автор

If you ran the KeyPressed() function twice, would it print "You clicked a button" twice since you are adding the same event args each time?

kaushikdr
Автор

what type of version is using? 1 .professional
2. community
?

healthpulsemind