views:

178

answers:

6

what events in c# and give clear real time example and program

+1  A: 

Events are the methods that you can call back at runtime from your code.

Ngu Soon Hui
ok nice answer i understood
ratty
+4  A: 

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).

Wim Hollebrandse
A: 

Wikipedia has a good overview of events

http://en.wikipedia.org/wiki/Event%5F%28computing)

Richard Ev
+7  A: 

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#:

or... you can just ask more specific questions here, and I'm sure somebody will help you understand the details.

Happy eventhandling.

Lasse V. Karlsen
+1  A: 

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.

Hans Passant