DELEGATES
This presentation will explain
about delegates in
C#(Csharp).
 Delegate is defined as a pointer to a
function.
 A delegate is an object that can refer to a
method.
 Therefore , when you create a delegate
,you are creating an object that can hold
a reference to a method.
 The delegate declaration specifies a
return type and parameter list.
 A delegate can invoke the method to which it
refers. Thus, the method that will be invoked
by a delegate is not determined at compile
time , but rather at runtime. This is the
advantage of a delegate.
 You can then initialize the variable as a
reference to any function that has the same
return type and parameter list as that
delegate.
 Delegate is a communication channel, it helps
us to call back after getting the results.
The general form of delegate declaration
is shown here:-
Syntax:
public delegate ret-type
delegatename(parameter-list);
• ret-type is the type of value returned by the methods
that the delegate will be calling. The name of the
delegate is specified by name.
• The parameters required by the methods called
through the delegates are specified in the
parameter-list.
Uses of delegates:-
• Delegates allow methods to be passed as
parameters.
• Delegates can be used to define call back
methods.
• Delegates are used to pass methods as
arguments to other methods.
• Delegates are like function pointers where
input parameter and return parameter should
be same type.
Program:-
using System;
namespace ConsoleApplication1
{
public delegate int myDel(int a, int b)
class Program
{
static void MethodAdd(int a, int b)
{
int c= a+b;
return c;
}
static void Main(string[] args)
{
myDel obj= new myDel(MethodAdd);
int c= obj(10,6)
Console.Writeline(“addition of two no. is: “ +c);
Console.ReadKey();
}
}
}
Output:-
addition of two no. is: 16
Types of Delegates:
There are two types of delegate.
1. Single delegate:-
• A delegate is called simple delegate if it
invokes a single method.
• Simple delegate refer to a single method.
2. Multicast delegate:-
• The Multicast delegates can invoke multiple
methods.
• Multicast delegates can send messages to
multiple clients/subscribers
• It uses += sign to broadcast multiple clients.
• It makes two way communication.
Program:-
using System;
namespace ConsoleApplication2
{
public delegate int myDel(int a, int b)
class Program
{
static void MethodAdd(int a, int b)
{
Console.Writeline(“The sum is: “ + (a+b));
}
static void MethodMul(int a, int b)
{
Console.Writeline(“The product is: “ + (a*b) );
}
static void Main(string[] args)
{
myDel obj= new myDel(MethodAdd);
obj += new myDel(MethodMul);
Console.ReadKey();
}
}
}

Explain Delegates step by step.

  • 1.
    DELEGATES This presentation willexplain about delegates in C#(Csharp).
  • 2.
     Delegate isdefined as a pointer to a function.  A delegate is an object that can refer to a method.  Therefore , when you create a delegate ,you are creating an object that can hold a reference to a method.  The delegate declaration specifies a return type and parameter list.
  • 3.
     A delegatecan invoke the method to which it refers. Thus, the method that will be invoked by a delegate is not determined at compile time , but rather at runtime. This is the advantage of a delegate.  You can then initialize the variable as a reference to any function that has the same return type and parameter list as that delegate.  Delegate is a communication channel, it helps us to call back after getting the results.
  • 4.
    The general formof delegate declaration is shown here:- Syntax: public delegate ret-type delegatename(parameter-list); • ret-type is the type of value returned by the methods that the delegate will be calling. The name of the delegate is specified by name. • The parameters required by the methods called through the delegates are specified in the parameter-list.
  • 5.
    Uses of delegates:- •Delegates allow methods to be passed as parameters. • Delegates can be used to define call back methods. • Delegates are used to pass methods as arguments to other methods. • Delegates are like function pointers where input parameter and return parameter should be same type.
  • 6.
    Program:- using System; namespace ConsoleApplication1 { publicdelegate int myDel(int a, int b) class Program { static void MethodAdd(int a, int b) { int c= a+b; return c; } static void Main(string[] args) { myDel obj= new myDel(MethodAdd); int c= obj(10,6) Console.Writeline(“addition of two no. is: “ +c); Console.ReadKey(); } } }
  • 7.
    Output:- addition of twono. is: 16 Types of Delegates: There are two types of delegate. 1. Single delegate:- • A delegate is called simple delegate if it invokes a single method. • Simple delegate refer to a single method.
  • 8.
    2. Multicast delegate:- •The Multicast delegates can invoke multiple methods. • Multicast delegates can send messages to multiple clients/subscribers • It uses += sign to broadcast multiple clients. • It makes two way communication.
  • 9.
    Program:- using System; namespace ConsoleApplication2 { publicdelegate int myDel(int a, int b) class Program { static void MethodAdd(int a, int b) { Console.Writeline(“The sum is: “ + (a+b)); } static void MethodMul(int a, int b) { Console.Writeline(“The product is: “ + (a*b) ); } static void Main(string[] args) { myDel obj= new myDel(MethodAdd); obj += new myDel(MethodMul); Console.ReadKey(); } } }