Introduction To ASP.NET Delegates and Events in C#
Today, We want to share with you Introduction To ASP.NET Delegates and Events in C#.In this post we will show you C# .NET Delegates and Events, hear for How to work with delegates in C# we will give you demo and example for implement.
In this post, we will learn about Delegates and Events in C# / .NET with an example.
A Delegates in c# can be considered as a reference type variable which can hold the reference to a method.
This reference can then be changed at runtime. It is mostly used for implementation of events and the call-back methods.
These are implicitly derived from the inbuilt System.Delegate class.Delegates are used to pass methods as arguments(parameter) to some another method.
Declaring Delegates
Delegate declaration determines the methods that can be referenced by it.
It can refer to a method, which has the same signature as that of the delegate.
For example, consider a delegate:
[php]
public delegate string MyDelegate (int s);
[/php]
The above delegate can be used to reference any method that has a single int parameter and returns a string type variable.
C# support two types of delegate.
- 1- Single Cast Delegates
- 2- Multi Cast Delegates
Delegates in c# 1- Single Cast Delegates
Single cast delegate means which hold an address of single method, which is explained in below example.
[php]
namespace DelegeteConsole
{
class Program
{
public delegate int DelegatSample(int a, int b); // Declaration of Delegate.
static void Main(string[] args)
{
MathsOperation _MathsOperation = new MathsOperation();
DelegatSample AddDelegate = MathsOperation.AddNumber;
int AddResult = _AddDelegate(10, 20);
Console.WriteLine(AddResult);
DelegatSample SubDelegate = MathsOperation.SubtractNumber;
int SubtractionResult = _SubDelegate(20, 10);
Console.WriteLine(SubtractionResult);
Console.ReadKey();
}
}
public class MathsOperation
{
public int AddNumber(int p_no1, int p_no2)
{
return p_no1 + p_no2;
}
public int SubtractNumber(int p_no1, int p_no2)
{
return p_no1 – p_no2;
}
}
}
[/php]
Delegates in c# 2 Multi Cast Delegates
Multi cast delegate is used to hold an address of multiple methods in a single delegate.To hold multiple addresses with delegate we will use overloaded += operator and if you want to remove addresses from delegate we need to use overloaded operator -=.Multicast delegates will work only for the methods which have return type only void.If we want to create a multicast delegate with return type we will get the return type of the last method in the invocation list.
[php]
namespace DelegeteConsole
{
class Program
{
public delegate void MultiDelegate(int a, int b);
static void Main(string[] args)
{
MathsOperation _MathsOperation = new MathsOperation();
MultiDelegate _MultiDelegate = MathsOperation.AddNumber;
_MultiDelegate += MathsOperation.SubNumber;
_MultiDelegate += MathsOperation.MulNumber;
_MultiDelegate(15, 10);
Console.ReadLine();
}
}
public class MathsOperation
{
public static void AddNumber(int p_no1, int p_no2)
{
Console.WriteLine(“Addition Value: ” + (p_no1 + p_no2));
}
public static void SubNumber(int p_no1, int p_no2)
{
Console.WriteLine(“Subtraction Value: ” + (p_no1 – p_no2));
}
public static void MulNumber(int p_no1, int p_no2)
{
Console.WriteLine(“Multiply Value: ” + (p_no1 * p_no2));
}
}
}[/php]
Read :
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Delegates and Events in C# / .Net Simplified.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.