Delegate, Multicast Delegate and Generic Delegate
Delegate | Multicast Delegate | Generic Delegate |
Multiple Examples and Live Results
Single Delegates Examples | Mapped Delegate Static and Instance Methods |
Multicast Delegate Examples | What are Generic Delegates, Types and Examples |
What is “Delegate”? What are different “Types” of Delegate? What is “Multicast Delegate”?What is “Generic Delegate”?
A Delegate is an object which holds the references to a method within delegate object.
A Delegate is a type-safe object.
A Delegate is invoking to methods in asynchronously manner.
A Delegate encapsulates a static method or instance methods.
A Delegate can be with parameters or without parameters.
Delegate’s benefits,
1) Improve the performance of application.
2) Encapsulate to methods call from caller.
3) Call a method asynchronously.
Three steps for using Delegates,
1) Declare
2) Create
3) Point
Syntax: - public delegate delegate_type delegate_name();
Types of Delegates,
1) Delegate
A delegate is use when you need to pass to single reference to a method within delegate object.
A multicast delegate is use when we needed to pass the more than one reference to a method within delegate object.
A multicast delegate is use when we needed to pass the more than one reference to a method within delegate object.
The basic examples of Delegates using in C# .Net as given below,
namespace delegateExample { public delegate int AddByDelegate(int var1, int var2); public class DelegateClass { public static int Add(int a, int b) { return a + b; } static void Main(string[] args) { //Creating the Delegate Instance AddByDelegate delObject = new AddByDelegate(Add); Console.Write("Please enter value"); int num1 = Int32.Parse(Console.ReadLine()); int num2 = Int32.Parse(Console.ReadLine()); // Here method Add is call. int Result = delObject(num1, num2); Console.WriteLine("Result :" + Result); Console.ReadLine(); } } }
//The Delegate - OutPut Result : 30
Example for Multicast Delegate as following,
namespace MulticastDelegateExample
{
public delegate int MulticastDelegateName(int num1, int num2);
public class MulticastClass
{
public static int Add(int num1, int num2)
{
return num1 + num2;
}
public static int Sub(int num1, int num2)
{
return num1 - num2;
}
static void Main(string[] args)
{
//Create the Add Delegate Instance
MulticastDelegateName objDelegt = new MulticastDelegateName(Add);
Console.WriteLine("Please enter numbers!");
int num1 = Int32.Parse(Console.ReadLine());
int num2 = Int32.Parse(Console.ReadLine());
int AddRest = objDelegt(num1, num2);
Console.WriteLine("Result :" + AddRest);
//Create the Sub Delegate Instance
objDelegt += new MulticastDelegateName(Sub);
int subRest = objDelegt(num1, num2);
Console.WriteLine("Result :" + subRest);
Console.ReadLine();
}
}
}
The Output as given below,
Please enter numbers!
num1 = 20
num2 = 10
Results: Add: 30 and Sub: 10
Microsoft provides three ready-made generics delegate. This is call shorter and sweeter delegate. This to minimize the code complexity of the apps.
1. Func< input, output >
2. Action< inputParameter >
3. Predicate< inputParameter >
Func <input, output > Delegate:-
The Func <input, output> generic delegate is use, when you need to some input parameter and then return some output.
The example for Func generic delegate as given below,
namespace GenericDelegate
{
public class Delegates
{
static void Main(string[] args)
{
// int is a input parameter and double is output parameter
Func<int, double> calPiR2Obj = r => 3.12 * r * r;
Console.Write(calPiR2Obj(4));
Console.ReadLine();
}
}
}
Action< inputParameter > Delegate:-
The Action< inputParameter> generics delegate is use, when you need to input parameter and no need to return output that means return void.
The example for Action generics delegate as below,
namespace GenericDelegate
{
public class Delegates
{
static void Main(string[] args)
{
Action<string> ActionObject = x => Console.WriteLine(x);
ActionObject("This is Action delegate!");
Console.ReadLine();
}
}
}
Predicate < inputParameter > Delegate:-
The Predicate generics delegate is use, when you need to input parameter and return output.
The return output is Boolean type just like true or false. This delegate is uses when you need to check the conditional statements are true or false.
The example for Predicate delegate as below,
namespace GenericDelegate
{
public class Delegates
{
static void Main(string[] args)
{
// string is input but out put is retun True or False
Predicate<string> checkConditionIsTF = x => x.Length > 10;
Console.WriteLine(checkConditionIsTF("Anil Kumar Singh"));
Console.ReadLine();
}
}
}
The example for Generic Delegates:-
namespace GenericDelegate
{
public class Delegates
{
static void Main(string[] args)
{
//Int is an input parameter and double is output parameter.
Func<int, double > calPiR2Obj = r => 3.12 * r * r;
Console.WriteLine(calPiR2Obj(4));
//String is input but no output return.
Action<string > ActionObject = x => Console.WriteLine(x);
ActionObject("This is Action delegate!");
//String is input but output, the return is true or false
Predicate< string > checkConditionIsTF = x => x.Length > 10;
Console.WriteLine(checkConditionIsTF("Anil Kumar Singh"));
Console.ReadLine();
}
}
}
I hope you are enjoying with this post! Please share with you friends. Thank you!!!