--> A delegate in C# is used to represent a function/(Method).
--> A delegate can be used to represent one or more functions(s)/(Method).
--> A delegate in C# is same as function pointer in C++.
--> If a delegate representing Only one Method then, it is known as Single Cast Delegate.
--> If a delegate representing more than one method(s) then it is known as multi cast Delegate.
--> If a delegate is representing a method without any name (Anonymous Method), then it is known as Anonymous Delegate.
We Use 3 Steps to work with Delegate:-
- Creating a Delegate
- Instantiating the Delegate
- Invoking the Delegate
Let us Consider a function like:->
public void Display(string S)
{
//Code
}
We use 3 steps of Delegate like:->
1. Creating a Delegate
Syntax:->
Access_Modifier Delegate Return_Type Delegate_Name([Parameter_List])
Example:->
public Delegate void SampleDelegate(string S);
2. Instantiating the delegate:->
Syntax:->
Delegate_Name Object_Name=new Delegate_Name(Target_Function_Name);
Example:->
SampleDelegate ObjD = new SampleDelegate(Display);
3. Invoking the Delegate:->
Syntax:->
Delegate_Object_Name([Parameter(s)_Value(s)]);
Example:->
ObjD("Welcome");
Example To Create a delegate to represent a method i.e. available in same class of Main() method:->
Create A Console Application with Name of Delegates and Rename FileName as Example1
Write below Code:=>
namespace Delegates
{
internal class Example1
{
static void Display(string S)
{
Console.WriteLine("Value is "+S);
}
delegate void SampleDelegate(string X);
static void Main(string[] args)
{
SampleDelegate ObjD = new SampleDelegate(Display);
ObjD("Welcome");
Console.ReadLine();
}
}
}
Example To Create a delegate to represent a method i.e. available in different class of Main() method:->
Adding a Class with Name of Example2
Write below Code:=>
namespace Delegates
{
class Sample
{
public void Square(int x)
{
Console.WriteLine("The Square Value of {0} is {1}",+x, (x*x));
}
}
//step1 Creating Delegate
delegate void SampleDelegate(int x);
internal class Example2
{
static void Main(string[] args)
{
Sample obj1 = new Sample();
//Step 2- Instantiate the Delegate
SampleDelegate objD = new SampleDelegate(obj1.Square);
//Step 3- Invoking the Delegate
objD(6);
Console.ReadLine();
}
}
}
Example of Multicasting Delegate:->
Adding a Class with Name of Example3
Write below Code:=>
namespace Delegates
{
class Arithmetic
{
public void Add(int a, int b)
{
Console.WriteLine("Sum is "+(a+b));
}
public void Subtract(int a, int b)
{
Console.WriteLine("Different is " + (a - b));
}
public void Multiply(int a, int b)
{
Console.WriteLine("Product is " + (a * b));
}
public void Divide(int a, int b)
{
Console.WriteLine("Quotient is " + (a / b));
}
}
//Step -1: Creating a Delegate
public delegate void MCDelegates(int a, int b);
internal class Example3
{
static void Main(string[] args)
{
Arithmetic obj1 = new Arithmetic();
//Step -2: Instantiating the Delegate
MCDelegates objD = new MCDelegates(obj1.Multiply);
objD += obj1.Add;
objD += obj1.Divide;
objD += obj1.Subtract;
Console.WriteLine("Please Enter 2 Numbers");
int x=Convert.ToInt32(Console.ReadLine());
int y=Convert.ToInt32(Console.ReadLine());
objD(x,y);
objD -= obj1.Add;
objD -= obj1.Divide;
objD(200, 40);
Console.ReadLine();
}
}
}

Comments
Post a Comment