Pages

Delegates and events


*   2.6 Delegates and events

   A delegate provides a way to encapsulate a method. An event is a notification that some action has occurred. Delegates and events are related because an event is built upon a delegate. Both expand the set of programming tasks to which C# can be applied.

*      2.6.1 Delegates

A delegate means to handover and also it real means that a method acting for another method. Delegate is a special type of object which holds reference of a method. C# implements call back technique with it much safer and object oriented manner.

A delegate object is a special type of object that contains the details of a method rather than data. Delegates in C# are used for two purposes:

§  Callback
§  Event handling

Using a special kind of object known as delegate object. Delegate is a class type object and it is used to invoke a method that was been created or encapsulated at the time of its creation.

A delegate declaration defines a class using the class System.Delegate as a base class. Delegate methods are any functions whose signature matches the delegate signature exactly.

The delegate instance holds the reference to delegate methods. The instance is used to invoke the methods indirectly.

   An important feature of a delegate is that it can be used to hold refer eve to a method of any class. The only requirement is that its signature must match the signature of the method.

   It is important to understand that the same delegate can be used to call different methods during the runtime of a program by simply changing the method to which the delegate refers. Thus, the method that will be invoked by a delegate is not determined at compile time, but rather at runtime. This is the principal advantage of a delegate.

   Four steps for creating and using delegate.

1.     Delegate declaration
2.     Delegate method declaration
3.     Delegate instantiation (Creating object for delegate)
4.     Delegate invocation (executing method)


Ø 2.6.1.1 Definition & declaration


1.    Delegate declaration


   A delegate type is declared using the keyword delegate. The general form of a delegate declaration is shown here:

  Syntax :- modifier delegate return-type delegatename(parameter-list);


   Delegate is the keyword that signifies that the declaration represents a class type derived from System.Delegate. The return type indicates the return type of the delegate.

   Parameters identify the signature of the delegate. The delegate name is any valid C# identifier and is the name of the delegate that will be used to instantiate delegate objects.

   The modifier controls the accessibility of the delegate. It is optional. The example of delegate declaration is as below

Ex.:-   Delegate void del_simple();


2.    Delegate method declaration

   The methods whose references are encapsulated into a delegate instance are known as delegate methods or callable entities. The signature and return type of delegate methods must exactly match the signature and return type of the delegate.

   One feature of delegates is type-safe to the extent that they ensure the matching of signatures of the delegate methods.

      Ex.:-   public void welcome();
                        OR
            Public static void welcome()


3.    Delegate instantiation (Creating object for delegate)

   Although delegates are of class types and behave like classes, C# provides a special syntax for instantiating their instances. A delegate-creation-expression is used to create a new instance of a delegate.

Syntax :- delegatename objname=new delegatename(argument/expressions);


   The delegatename is the name of the delegate declared earlier whose object is to be created. The expression must be a method name of a value of a delegatename.

   The argument and return type of the method must be same as that of delegate. If no method exists or more than one method exists than error occurs.



   The matching method can be static or non-static. If it is an instance method we need to specify the instance as well as the name of the method. A method and delegate name to which a delegate refers are known when the delegate is instance created.

4.    Delegate invocation (executing method)

   C# uses a special syntax for invoking a delegate when a delegate is invoked, it in turn invokes the method whose reference has been encapsulated into the delegate,(only if their signatures match). Invocation takes the following form:

Syntax:-   delegate_object(parameters list);


   The optional parameters list provides values for the parameters of the method to be used.


No comments:

Post a Comment