Saturday, January 10, 2009


1.True foundation of events is delegates.

2.Events are declared using delegates

3.There are two important terms with respect to events. The event source and the event receiver

4.The object that raises the event is called event source and the object that responds to the event is called event receiver. The communication channel between an event source and an event receiver is the delegate .

5.What is Delegate ?

Ans: Delegates act as an intermediary between an event source and an event destination.

Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type.

You can encapsulate any matching method in that delegate.

To be even precise delegates are similar to function pointers.

They can be called as type safe function pointers.

Unlike function pointers, delegates are object-oriented and type safe.

A delegate class used as an intermediary between an event source and event receiver is called an event handler.

Delegates can be considered as 'function pointers' which is used to represent or invoke functions.

A delegate in C# is similar to a function pointer in C or C++.

A delegate in C# allows you to pass methods of one class to objects of other classes that can call those methods.

You can pass method m in Class A, wrapped in a delegate, to class B and Class B will be able to call method m in class A.

The concept of delegate was introduced in Visulal J++ and then carried over to C#.


What is the Function Pointer ?

Answer : A function pointer is a variable that stores the address of a function that can later be called through that function pointer.

This is useful because functions encapsulate behavior.

For instance, every time you need a particular behavior such as drawing a line, instead of writing out a bunch of code, all you need to do is call the function. But sometimes you would like to choose different behaviors at different times in essentially the same piece of code.

A function pointer is a type of pointer in C, C++,

In programming languages like C, function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks.

One aspect in the case of late-binding is runtime

Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, that a running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing else than an address. It is only important how you, or better your compiler/processor, interpret the memory a pointer points to.

Important note: A function pointer always points to a function with a specific signature! Thus all functions, you want to use with the same function pointer, must have the same parameters and return-type!

Programmers often need to implement callbacks. I will discuss the fundamentals of function pointers and show how to use them to implement callbacks.

A callback function is one that is not invoked explicitly by the programmer; rather the responsibility for its invocation is delegated to another function that receives the callback function's address.

Example Uses of Function Pointers

Functions as Arguments to Other Functions :

If you were to write a sort routine, you might want to allow the function's caller to choose the order in which the data is sorted; some programmers might need to sort the data in ascending order, others might prefer descending order while still others may want something similar to but not quite like one of those choices. One way to let your user specify what to do is to provide a flag as an argument to the function, but this is inflexible; the sort function allows only a fixed set of comparison types (e.g., ascending and descending).

A much nicer way of allowing the user to choose how to sort the data is simply to let the user pass in a function to the sort function. This function might take two pieces of data and perform a comparison on them. We'll look at the syntax for this in a bit.

Callback Functions :

Another use for function pointers is setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.

Why would you ever write code with callback functions? You often see it when writing code using someone's library. One example is when you're writing code for a a graphical user interface (GUI). Most of the time, the user will interact with a loop that allows the mouse pointer to move and that redraws the interface. Sometimes, however, the user will click on a button or enter text into a field. These operations are "events" that may require a response that your program needs to handle. How can your code know what's happening? Using Callback functions! The user's click should cause the interface to call a function that you wrote to handle the event.

To get a sense for when you might do this, consider what might happen if you were using a GUI library that had a "create_button" function. It might take the location where a button should appear on the screen, the text of the button, and a function to call when the button is clicked. Assuming for the moment that C (and C++) had a generic "function pointer" type called function, this might look like this:
void create_button( int x, int y, const char *text, function callback_func );

Whenever the button is clicked, callback_func will be invoked. Exactly what callback_func does depends on the button; this is why allowing the create_button function to take a function pointer is useful.

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

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

8.Events are declared using delegates

9.Clicking the button is normally referred to as an 'Event' and all the associated functions that are called as a result of an event as 'Event Handlers'

10.Events are an important building block for creating classes that can be reused in a large number of different programs

11. Event handler is a method that has the same signature as the event and this method is executed when the event occurs.

12.To define an event you need first to define a delegate that contains the methods that will be called when the event raised, and then you define the event

based on that delegate.

13.D.What is Events ?

Event is the part of even-driven progarming language.It is part of Grafical User Interface(GUI) .

Events are handled by Delegate.

Events has two arguent: first is object and second is EventArgs

Object is reconized which button is being clicked because there are lots of buttion in the page. Object parameter recognize source of the event. And on the


In general , every code which we write is placed in the event. like page_load event or button click event and when event is fired the code is executed.Every

code is placed in the event that why it is said is event driven.And when ever we click button the event is fired and the code is postback(submit ) to

the server.And this every develor knows wery well but how button do this does not know. For knowing we have to knnow about Delegates and Eventts. Events

occures on the page. When a user interacts with a GUI control (e.g., clicking a button on a form), one or more methods are executed in response to the

above event.

14.Event Handlers in C# :

An event handler in C# is a delegate with a special signature, given below.

public delegate void MyEventHandler(object sender, MyEventArgs e);

The first parameter (sender) in the above declaration specifies the object that fired the event.

The second parameter (e) of the above declaration holds data that can be used in the event handler.

The class MyEventArgs is derived from the class EventArgs.

Dev said : Event Handler is a method where we write to code.

event handlers (methods with the same signature as that of the delegate declaration).

Still, it is better to know what is going on under the hood.

15.DEV : i am still confused how event and delegates are related , i only understand that Delegates are the that pass methods of one class to object of other

class taht call those methods.I too undersatand about the Event Handler, Event Handler is method with two parameters where we place our code to execute.

16. 'notified' (this simply means that all the event handler functions are invoked).

17.We can now look at how events are implemented in C# and how it is associated with event handlers.

18.DEV UNDERSTAND : fOR evnet three things are impartant :

First Event Handlers function -

public void onButtonClick (object source, int clickCount) {
//Define the actions that should be performed.

Second Delegates -

The delegate declaration will be as follows:

public delegate void ButtonEventHandler(object source, int clickCount)

where ButtonEventHandler is the delegate name.

Third is Event -

The event declaration will be as follows:

public event ButtonEventHandler ButtonClick;

The event declaration should have the keyword 'event' followed by the delegate type.

DEV SAID : We can see the paramerter of Event Handler Function and Delegates are the same - object sender and int count .

After taht the next step is the associating the Event handler function (onButtonAction) with the event (ButtonClick).

This is to be done because the ButtonClick event should know about the functions that are to be executed when the event is triggered

The operator += is used to achieve this as shown in the following code:

b.ButtonClick += new ButtonEventHandler(onButtonAction);

Here, b is an instance of the class Button. (Also, note that the onButtonAction should have the same parameter and return types as ButtonEventHandler


The operator += is used to achieve this as shown in the following code:

b.ButtonClick += new ButtonEventHandler(onButtonAction);

Here, b is an instance of the class Button. (Also, note that the onButtonAction should have the same parameter and return types as ButtonEventHandler


Now consider that the event is triggered. This can be done by the user or another process. In our case, the event is triggered when a user clicks on any of

the buttons. On doing this, all the associated functions (onButtonAction in the current context) of ButtonClick event should be executed. This can be

performed using the following code:

ButtonClick(this, count)

The parameters of ButtonClick should match with the delegate type (ButtonEventHandler).

19. How Event works?

Whenever an event is defined for a class, the compiler generates three methods that are used to manage the underlying delegate:

add_ :
this is a public method that calls the static Combine method of System.Delegate in order to add another method to its internal invocation list. This method

is however not used explicitly. The same effect is achieved by using the += operator as specified before.

remove_ :
this is also a public method that calls the static Remove method of System.Delegate in order to remove a receiver from the event's invocation list. This

method is also not called directly. Its job is done by the "-=" operator.

raise_ :
a protected method that calls the compiler generated Invoke method of the delegate, in order to call each method in the invocation list.

20.Dev Said : Signature is impartant for Delegates. It should match whith the method or function which is being passed.

The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have publishers who will do some logic and publish an "event." Publishers will then send out their event only to subscribers who have subscribed to receive the specific event.

In C#, any object can publish a set of events to which other applications can subscribe. When the publishing class raises an event, all the subscribed applications are notified. The following figure shows this mechanism.



The following important conventions are used with events:

Event Handlers in the .NET Framework return void and take two parameters.
The first paramter is the source of the event; that is the publishing object.
The second parameter is an object derived from EventArgs.
Events are properties of the class publishing the event.
The keyword event controls how the event property is accessed by the subscribing classes.

What is Event ?

An event is a placeholder for code that is executed when the event is triggered, or fired. Events are fired by a user action, program code, or by the system.

From a more formal perspective—and the C# point of view—an event is an object member, specifically a method

This Link Wolud Explane All :

No comments: