.Net

What are Delegates and what are the uses of the Delegates

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the developer to encapsulate a reference to a method inside a delegate object.

Delegates are basically used in these situations:

  • These are used to represent or refer to one or more functions.
  • These can only be used to define call-back methods.
  • In order to consume a delegate, we need to create an object to delegate.

Syntax of delegates is :

[modifier] delegate [returntype] [delegatename] ([parameterlist]);

modifier: It is the required modifier which defines the access of delegate and it is optional to use.

delegate: It is the keyword which is used to define the delegate.

returntype: It is the type of value returned by the methods which the delegate will be going to call. It can be void. A method must have the same return type as the delegate.

delegatename: It is the user-defined name or identifier for the delegate.

parameterlist: This contains the parameters which are required by the method when called through the delegate.

Example of simple delegate:

class Program {  
        static void DisplayName(string S) {  
            Console.WriteLine("My Name is :" + S);  
        }  
        delegate void func(string a);  
        static void Main(string[] args) {  
            func object= new func(DisplayName);  
            object("Code Hunger");  
            Console.Read();  
        }  
    }  

There are two types of delegates:

  • Single Cast Delegate
  • Multi Cast Delegate

Single Cast Delegate

A delegate that represents only a single function is known as Single Cast Delegate.

Multi Cast Delegate

A delegate that represents more than one function is known as Multi Cast Delegate.

Example of multicast delegates:

 class Program {  
        public void Addition(int a, int b) {  
            Console.WriteLine("Sum is:" + (a + b));  
        }  
        public void Subtraction(int a, int b) {  
            Console.WriteLine("Difference is:" + (a - b));  
        }  
        public void Multiplication(int a, int b) {  
            Console.WriteLine("Product is:" + (a * b));  
        }  
        public void Division(int a, int b) {  
            Console.WriteLine("Quotient is:" + (a / b));  
        }  
    }  
    public delegate void MultiCastDelegate(int a, int b);  
    class ExampleDelegate {  
        static void Main() {  
            Program object = new Program();  
            MultiCastDelegate obj= new MultiCastDelegate(object.Multiply);  
            obj += object.Add;  
            obj += object.Substract;  
            obj += object.Divide;  
            obj(40, 10);  
            obj -= object.Add;  
            obj -= object.Divide;  
            obj(50, 10);  
            Console.ReadLine();  
        }  
    }  

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button