Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Delegates and Event Handling

Delegates is Object Like EventHandler is a Delegate inherited from System.Delegate


Class
Delegate is like Function Pointer which contains references to method
Delegate to be used in program with a particular Prototype and the method whose
reference is to be passed to delegate must be of same prototype
More then one method can be passed as a reference through Delegates.Such
Delgates are known as Multi cast Delegates

Call a Function directly - No Delegate


In most cases, when we call a function, we specify the function to be called directly. If the
class MyClass has a function named Process, we'd normally call it like this
(SimpleSample.cs):
using System;
namespace Akadia.NoDelegate
{
public class MyClass

{
public void Process()
{
Console.WriteLine("Process() begin");
Console.WriteLine("Process() end");
}
}
public class Test
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.Process();
}
}
}
That works well in most situations. Sometimes, however, we don't want to call a function
directly - we'd like to be able to pass it to somebody else so that they can call it. This is
especially useful in an event-driven system such as a graphical user interface, when I want
some code to be executed when the user clicks on a button, or when I want to log some
information but can't specify how it is logged.
public delegate void SimpleDelegate ()
This declaration defines a delegate named SimpleDelegate, which will encapsulate
any method that takes no parameters and returns no value.
A delegate will allow us to specify what the function we'll be calling looks
like without having to specify whichfunction to call. The declaration for a delegate
looks just like the declaration for a function, except that in this case, we're declaring the
signature of functions that this delegate can reference.
There are three steps in defining and using delegates:
o
o
o

Declaration
Instantiation
Invocation

A very basic example (SimpleDelegate1.cs):


using System;
namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()

{
Console.WriteLine("I was called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
Console.ReadKey();
}
}

Output:

Calling More Then 1 Delegates(Multicast Delegates)

//Calling More Then 1 Delegates


using System;
delegate void MyDelegate(string s);
class MyClass
{
public static void Hello(string s)
{
Console.WriteLine(" Hello, {0}!", s);
}
public static void Goodbye(string s)
{
Console.WriteLine(" Goodbye, {0}!", s);
}
public static void Main()
{
MyDelegate a, b, c, d;
// Create the delegate object a that references
// the method Hello:
a = new MyDelegate(Hello);
// Create the delegate object b that references
// the method Goodbye:
b = new MyDelegate(Goodbye);
// The two delegates, a and b, are composed to form c:
c = a + b;//c is Multicast Delegate
// Remove a from the composed delegate, leaving d,
// which calls only the method Goodbye:
d = c - a;
Console.WriteLine("Invoking
a("A");
Console.WriteLine("Invoking
b("B");
Console.WriteLine("Invoking
c("C");
Console.WriteLine("Invoking
d("D");
}
}
Output:

delegate a:");
delegate b:");
delegate c:");
delegate d:");

Basic Mathematical Operation using Delegates

using System;
namespace Delegates
{
public delegate int DelegateToMethod(int x, int y);
public class Math
{
public static int Add(int first, int second)
{
return first + second;
}
public static int Multiply(int first, int second)
{
return first * second;
}
public static int Divide(int first, int second)
{
return first / second;
}
}
public class DelegateApp
{
public static void Main()

{
DelegateToMethod aDelegate = new DelegateToMethod(Math.Add);
DelegateToMethod mDelegate = new DelegateToMethod(Math.Multiply);
DelegateToMethod dDelegate = new DelegateToMethod(Math.Divide);
Console.WriteLine("Calling the method Math.Add() through the aDelegate
object");
Console.WriteLine(aDelegate(5,5));
Console.WriteLine("Calling the method Math.Multiply() through the
mDelegate object");
Console.WriteLine(mDelegate(5,5));
Console.WriteLine("Calling the method Math.Divide() through the
dDelegate object");
Console.WriteLine(dDelegate(5,5));
Console.ReadLine();
}
}
}

Output:

You might also like