Delegate
A delegate in C# is similar to a function pointer in C or C++.
Using a delegate allows the programmer to encapsulate a reference to a method
inside a delegate object. The delegate object can then be passed to code which
can call the referenced method, without having to know at compile time which
method will be invoked.
1. Delegate is a type safe function pointer.
2.
Delegate holds a reference (Pointer) to a function.
3. The
signature of the delegate must match the signature of the function, the
delegate points to, otherwise you get a compiler error.
4.
Delegate is similar to a class.
5 You
can create an instance of it.
Declaring Delegates
Without Parameter
public delegate void HelloDelegate();
With Parameter
public delegate void ParameteriseHelloDelegate(string Message);
Creating function that match the signature of the delegates
Without Parameter
public static void Hello()
{
Console.WriteLine("Simple
- Hello World");
}
With Parameter
public static void Hello(string Message)
{
Console.WriteLine("Parameterise
- " + Message);
}
Creating instance of delegates
Invoke Without Parameter Delegate
HelloDelegate simpleDelegate = new HelloDelegate(Hello);
simpleDelegate();
Invoke With Parameter Delegate
ParameteriseHelloDelegate ParameterDelegate
= new ParameteriseHelloDelegate(Hello);
ParameterDelegate("Hello World");
Output
Without Parameter Delegate
With Parameter Delegate
Multicast delegate
1. A Multicast delegate is a delegate that has reference to more
than one function
2. When
you invoke a multicast delegate, all the functions the delegate is pointing to
are invoked.
3. A
multicast delegate, invokes the methods in the invocation list, in the same
order in which they are added.
4.
Register & Un-Register a method with the delegate
+ Or +=
to register a method with the delegate
- Or -=
to unregister a method with the delegate
Declaring Delegate
public delegate void HelloDelegate();
Creating function that match the signature of the delegate
public static void HelloOne()
{
Console.WriteLine("Hello
World One");
}
public static void HelloTwo()
{
Console.WriteLine("Hello
World Two");
}
public static void HelloThree()
{
Console.WriteLine("Hello
World Three");
}
Creating instance of delegate
HelloDelegate multicastDelegate1
= new HelloDelegate(HelloOne);
HelloDelegate multicastDelegate2
= new HelloDelegate(HelloTwo);
HelloDelegate multicastDelegate3
= new HelloDelegate(HelloThree);
HelloDelegate multicastDelegate4
= multicastDelegate1 + multicastDelegate2 - multicastDelegate3;
multicastDelegate4();
OR
HelloDelegate multicastDelegate = new HelloDelegate(HelloOne);
multicastDelegate += HelloTwo;
multicastDelegate -= HelloThree;
multicastDelegate();
Output
Complete Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Delegate
{
class Program
{
/**************Simple
Delegate***************/
public delegate void HelloDelegate();
//public
delegate void HelloDelegate(string Message); // Error - Delegate can't be
overloaded
public static void Hello()
{
Console.WriteLine("Simple
- Hello World");
}
/**************Simple
Delegate***************/
/**************Parameterise
Delegate***************/
public delegate void ParameteriseHelloDelegate(string Message);
public static void Hello(string Message)
{
Console.WriteLine("Parameterise
- " + Message);
}
/**************Parameterise
Delegate***************/
/**************Multicast
Delegate***************/
public static void HelloOne()
{
Console.WriteLine("Hello
World One");
}
public static void HelloTwo()
{
Console.WriteLine("Hello
World Two");
}
public static void HelloThree()
{
Console.WriteLine("Hello
World Three");
}
/**************Multicast
Delegate***************/
public static void Main(string[]
args)
{
/**************Simple
Delegate***************/
HelloDelegate simpleDelegate
= new HelloDelegate(Hello);
simpleDelegate();
/**************Simple
Delegate***************/
/**************Parameterise
Delegate***************/
ParameteriseHelloDelegate ParameterDelegate
= new ParameteriseHelloDelegate(Hello);
ParameterDelegate("Hello
World");
/**************Parameterise
Delegate***************/
/**************Multicast
Delegate***************/
/*
+
Or += to register a method with the delegate
-
Or -= to unregister a method with the delegate
*/
HelloDelegate multicastDelegate1
= new HelloDelegate(HelloOne);
HelloDelegate multicastDelegate2
= new HelloDelegate(HelloTwo);
HelloDelegate multicastDelegate3
= new HelloDelegate(HelloThree);
HelloDelegate multicastDelegate4
= multicastDelegate1 + multicastDelegate2 + multicastDelegate3;
multicastDelegate4();
HelloDelegate multicastDelegate
= new HelloDelegate(HelloOne);
multicastDelegate
+= HelloTwo;
multicastDelegate
-= HelloThree;
multicastDelegate();
/**************Multicast
Delegate***************/
Console.ReadLine();
}
}
}
No comments:
Post a Comment