C# .NET Delegates and Events Simplified

C# .NET Delegates and Events Simplified

Today, We want to share with you C# .NET Delegates and Events Simplified.
In this post we will show you Delegates and Events in C#, hear for C# .NET Delegates and Events we will give you demo and example for implement.
In this post, we will learn about Events and Delegates Simplified with an example.

Delegate in c#

A delegate can be consider as a reference type variable which can holds the reference to a method.

This reference can then be changed at runtime.

Delegates are 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 methods.

Declaring Delegates

Delegate declaration determines the methods that can be referenced by the delegate.

A delegate can refer to a method, which has the same signature as that of the delegate.

For example, consider a delegate:

public delegate string MyDelegate (int s);

– The above delegate can be used to reference any method that has a single int parameter and returns an string type variable.

Syntax for delegate declaration is:

delegate   

C# support two types of delegate.

1- Single Cast Delegates
2- Multi Cast Delegates

1- Single Cast Delegates

– Single cast delegate means which hold address of single method, which is explained in below example.

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;
        }
    }
}

2- Multi Cast Delegates

– Multi cast delegate is used to hold address of multiple methods in single delegate.

– To hold multiple addresses with delegate we will use overloaded += operator and if you want 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 last method in the invocation list

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));
        }
    }
}

C# Programming/Delegates and Events

We hope you get an idea about C# Programming/Delegates and Events
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

Leave a Comment