Delegate is a C# language element
that allows you to reference a method. Delegate is basically a function
pointer. A delegate will allow us to specify
what the function we'll be calling looks like without having to
specify which function to call. The declaration for a delegate looks just like
the declaration for
a function.All delegates are implicitly derived from the
System.Delegate class.Delegates are type-safe, object-oriented and secure.
Why developers will use delegate?
Delegates are especially used for implementing events and the
call-back methods.
It giving you maximum flexibility to implement any functionality
you want at runtime.
if you have multiple methods with same signature (return type
& number of parameters) and want to call all the methods with single object
then we can go for delegates.
Delegates are used to pass methods as arguments to other methods.
Delegates can call methods synchronously and asynchronously.
There are three steps in defining and using delegates:
• Declaration
• Instantiation
• Invocation
Delegates are two types
- Single Cast Delegates
- Multi Cast Delegates
Single Cast Delegates
Single cast delegate means which hold address of single method
like :
public delegate double Singel_delegate(int a,int b);
//Declaration Of delegate
class Class1
{
static double simple_fn(int val1,int val2)
{
return val1*val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Singel_delegate delObj = new
Singel_delegate(simple_fn); //Initialization of delegates
Console.Write("Please Enter
Values");
int v1 =
Int32.Parse(Console.ReadLine());
//Passing values to Parameter
int v2 =
Int32.Parse(Console.ReadLine());
//Passing values to Parameter
//use a delegate for processing
double res = delObj(v1,v2);
//Invoke delegate
Console.WriteLine ("Result
:"+res);
//Output
Console.ReadLine();
}
}
Multicast Delegates
Multi cast delegate is used to hold address of multiple methods in
single delegate. To hold multiple addresses with delegate we will use
overloaded += operator
and if you want remove addresses from delegate we need to use
overloaded operator -=
Multicast delegates will work only for the methods which have
return type only void. If we want to create a multicast delegate with return
type we will get the
return type of last method in the invocation list.
public delegate void MultiCastDelegate(int a,int b);
public class Sampleclass
{
public static void Add(int x, int y)
{
Console.WriteLine("Addition Value: "+(x + y));
}
public static void Sub(int x, int y)
{
Console.WriteLine("Subtraction Value: " + (x - y));
}
public static void Mul(int x, int y)
{
Console.WriteLine("Multiply Value: " + (x * y));
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();
MultiDelegate del = Sampleclass.Add;
del += Sampleclass.Sub;
del += Sampleclass.Mul;
del(10, 5);
Console.ReadLine();
}
}
Output
Whenever we run above code we will get output like as shown below
Addition Value : 15
Subtraction Value : 5
Multiply Value: 75
Events
Events are user actions such as key press, clicks, mouse
movements, etc., or some occurrence such as system generated notifications.
Applications need to respond to events when they occur.
A C# event is a class member that is activated whenever the event
it was designed for occurs. Events and delegates work hand-in-hand to provide a
program’s
functionality. It starts with a class that declares an event. Any
class, including the same class that the event is declared in, may register one
of its methods for
the event. This occurs through a delegate, which specifies the
signature of the method that is registered for the event. The delegate may be
one of the pre-defined
.NET delegates or one you declare yourself. Whichever is
appropriate, you assign the delegate to the event, which effectively
registers the method that will be
called when the event fires.An event is a two-step process. First,
you need to define a delegate type that will hold the list of methods to be
called when the event
is fired. Next, you declare an event using the event
keyword. To illustrate the event, we are creating a console application. In
this iteration, we will define an
event to add that is associated to a single delegate
DelEventHandler
simple example of event in c#
using System;
public delegate void EventHandler();
class Program
{
public static event EventHandler _fire;
static void Main()
{
// Add event handlers to
Show event.
_fire += new
EventHandler(fn);
_fire += new
EventHandler(fn1);
_fire += new EventHandler(fn2);
// Invoke the event.
_fire.Invoke();
}
static void fn()
{
Console.WriteLine("fn
Call");
}
static void fn1()
{
Console.WriteLine("fn1
Call");
}
static void fn2()
{
Console.WriteLine("fn2
Call");
}
}
Output
fn Call
fn1 Call
fn2 Call