Delegate in C#
A delegate is a reference type that basically encapsulates the reference of methods. It is similar to the class that stores the reference of methods which have the same signature as a delegate has.
You can also call the delegate as a type-safe function pointer (OR A delegate in C# is similar to a function pointer in C or C++).
Delegates are called Type Safe because they only hold the reference of the same method which returns the type and number of the return type of the method and its delegate.
The delegate is called function pointer because it refers to the function and can be executed by a function.
Like any other reference type, the instance (objects) of the delegate is also created. You can associate the instance of the delegate with the corresponding signature method. After that, you can call that method by delegate instance.
All delegate types are derived from the delegate class of .NET Framework. The delegate type is sealed and cannot be derived.
The most important feature of the Delegate is that by delegate you can pass a method as a parameter inside the second method. Ability to pass the delegate method as a parameter gives you the ability to define Callback Methods.
Callback methods are such methods that occur on the completion of a particular event. As if any event is generated, methods can be automatically callback by the delegate.
There are three steps in defining and using delegates :
Declaration
Instantiation
Invocation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication20
{
public delegate string printDelegate();
class delegateDemo
{
public string printMethod()
{
return "Hello Delegate";
}
public string deligateMethod(printDelegate del)
{
string msg;
msg = del();
return msg;
}
}
class Program
{
static void Main(string[] args)
{
string msg;
delegateDemo dd = new delegateDemo();
printDelegate del = new printDelegate(dd.printMethod);
msg = dd.deligateMethod(del);
Console.WriteLine(msg);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
public delegate void demoDelegate();
class delegateMethodClass
{
public void hello()
{
Console.Write("Hello Mr. Delegate");
}
public void welcome()
{
Console.WriteLine(" Welcome in Program");
}
}
class Program
{
static void Main(string[] args)
{
delegateMethodClass dmc = new delegateMethodClass();
demoDelegate dd1 = dmc.hello;
demoDelegate dd2 = dmc.welcome;
dd1 += dd2;
dd1();
}
}
}
Multicasting Delegate :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication22
{
public delegate void myDelegate();
class Program
{
public static void Method1()
{
Console.WriteLine("Inside Method 1...");
}
public static void Method2()
{
Console.WriteLine("Inside Method 2...");
} public static void Method3()
{
Console.WriteLine("Inside Method 3...");
}
static void Main(string[] args)
{
myDelegate d=null;
d += Method1;
d += Method2;
d += Method3;
d.Invoke();
Console.ReadLine();
}
}
}
If read more about delegate, please follow these links -
1). https://www.mindstick.com/articles/139/delegates
2). https://www.mindstick.com/articles/20/delegates
3). https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp
4). https://www.mindstick.com/articles/851/events-with-delegate
5). https://www.mindstick.com/articles/852/use-delegate-in-winform
6). https://www.mindstick.com/articles/951/delegate-in-c-sharp
7). https://www.mindstick.com/articles/954/delegate-in-c-sharp-dot-net
8). https://www.mindstick.com/articles/11963/delegate-and-events-in-c-sharp
and so on
https://www.mindstick.com/DeveloperSection/Search?s=DELEGATE&type=article
Leave Comment