Understand delegate in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 5405 red flag

How to use delegate in C#

Understand delegate in C#

In this article we will try to understand what is delegate and how to use it in C# application. If you have experience of C and C++ then you might aware of function pointer. Delegate is very similar with that. We can say that delegate is advance version of function pointer; it is smarter than function pointer.

It is type safe in nature. Now, how it type safe? If we create one function pointer then using that we can invoke any type of function. Any type of function means function with different argument length and type.

But in delegate we cannot do that. We have to create particular type of delegate to invoke particular function. Let’s see how to do that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        public static void Hello()
        {
            Console.WriteLine("I am Hello function.");
        }
        public delegate void DelHello();
        static void Main(string[] args)
        {
            DelHello A = Hello;
A.Invoke();
 
            Console.ReadLine();   
        }
    }
}
 

First of all we have create Hello function which neither takes argument nor return any value, and below of hello function we have created DelHello delegate to invoke Hello() function. To invoke function we have to use Invoke method of delegate object.

Here is output


Now, if we want create delegate of such function which takes argument we have to make slide modification of above code. Have a look on below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        public static void Hello(int A)
        {
            Console.WriteLine("Value of A is ="+ A );
        }
        public delegate void DelHello(int A);
        static void Main(string[] args)
        {
            DelHello A = Hello;
A.Invoke(100);
            Console.ReadLine();   
        }
    }
}

Here we have changed signature of Hello() function. Currently it is taking one integer argument. To invoke this function we have defined DelHello like below.

        public delegate void DelHello(int A);

Now, it can able to call such a function which will take one integer argument.  Here is sample output.


As we told, this delegate (new DelHello)  can able to call all the function which takes single integer value as argument. Now, we will call more than one function using single delegate. Have a look on below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        public static void Hello(int A)
        {
            Console.WriteLine("In Hello. Value of A is ="+ A );
        }
        public static void Hello1(int B)
        {
            Console.WriteLine("In Hello1. Value of A is =" + B);
        }
 
 
        public delegate void DelHello(int A);
        static void Main(string[] args)
        {
            DelHello A = Hello;
            A+= Hello1;
A.Invoke(100);
            Console.ReadLine();   
        }
    }
}

Here there are two functions with same type of signature (both takes integer argument, but function name different).  We have used + operator to assign both function to single delegate object.


Conclusion :-
In this article we have learned how to use delegate to call function in C#. Hope you have understood the concept.


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)