Events In C# - Definition and Example [closed]
What are events in C#
? Can you give me a clear real time example and program?
An event is a hook on an object where the object can say "Hey, something interesting is about to happen" (or just happened), and code on the outside of the object can say (before that something happens) that "I'm interested in that message".
Take buttons for instance, this code:
btOK.Click += new EventHandler(btOK_Click);
This says to the button: Hey, when you want to fire the Click event (which is fired when the user clicks on the button), let me know by calling my method, btOK_Click.
You can think of it another way. Let's say that you have a yellow postit note attached to your monitor that says "Whenever the code stops compiling, please call Frank", that's an event. What Frank does when you call him, that's the "Event Handler", the "code" that runs in response to your event.
So the terms are:
- Event: A hook on an object where code outside of the object can say "When that something-something happens, that fires this event, please call my code"
- Event Handler: The code that is called when the event fires
- Firing an event: Basically the same as calling it, it's just a different word for essentially the same thing
There's plenty of information on the web about events and .NET/C#:
- MSDN: Events Tutorial
- C# Help: C# Events
- Devhood: Introduction to C# Events
- C#-Station: Introduction to delegates and events
or... you can just ask more specific questions here, and I'm sure somebody will help you understand the details.
Happy eventhandling.
Try the C# Events Tutorial on MSDN.
Abstract:
An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button).
Events are the methods that you can call back at runtime from your code.
An event is an accessor for a delegate object, just like a property is an accessor for a field. The accessors are named "add" and "remove" instead of "get" and "set". One difference is that the compiler automatically generates accessors if you don't provide your own.
You can't understand what an event really does until you understand what a delegate does. Google away on that keyword.
Wikipedia has a good overview of events
http://en.wikipedia.org/wiki/Event_(computing)
-msdn Description- An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object.
- C#help Descreption-An event is defined in C# as 'a member thatenables an object or class to provide notifications'
And you can try the ALL-Mighty google search
精彩评论