What is Delegate in C# with example?

0 votes
36 views
Ethan Karla asked 20 days ago in Programming Language by Ethan Karla

What do you mean by Delegate in C#? Explain it with the help of Delegate in C#?

1 Answer

0 votes
Ravi Vishwakarma answered 20 days ago by Ravi Vishwakarma
Delegates
 Delegates are similar to function pointer. We can pass a function as a parameter so we call the delegation. The delegate is a reference type of datatype this pass the function as a parameter. That can refer the function as arguments in delegation function.
Delegate have some properties
  1. Delegates are similar to C++ function pointers, but are type safe.
  2. Delegates allow methods to be passed as parameters.
  3. Delegates can be used to define callback methods.
  4. Delegates can be chained together; for example, multiple methods can be called on a single event.
  5. Methods don't need to match the delegate signature exactly.
  6. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code that can call the referenced method, without having to know at compile time which method will be invoked.
  7. An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references.
  8. In delegate add and subtract (+ or -) on function. In other word delegate that hold the more than on method.

Delegation in C# has two type

1. Simple delegation
2. Multicast delegation

Simple delegation example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1 {
    // create the delegate in namespace
   public delegate void MyDelName(String name);
   public delegate void MyDelegateAge(int age );
    //creating the program class in namespace
   public class Program {
      static void Main(string[] args)
      {
          // create the object of class FileDemo
          FileDemo fDemo = new FileDemo();
          // before apply the delegation print the fDemo onject as string
          Console.WriteLine(fDemo +'\n' );
          // create the first delegate object for storing the setName method as argument in delegation
          MyDelName setNameDel = new MyDelName(fDemo.setName);
          setNameDel('Shivam Prajapati');
          setNameDel = new MyDelName(fDemo.setFatherName);
          setNameDel('Santosh Kumar');
          // and second delegation object for string the setAge method as argument in delegation
          MyDelegateAge setNameAge = new MyDelegateAge(fDemo.setAge);
          setNameAge(23);
          // after apply the delegation print the fDemo onject as string
          Console.WriteLine(fDemo + '\n');
          Console.ReadKey();
      }
  }
    //craeting the file demo class in namepace
  class FileDemo
  {
      // fields
      private String name;
      private String fatherName;
      private int age;
      // methods
      public FileDemo()
      {
          this.name = 'name';
          this.age = 0;
          this.fatherName = 'father name';
      }
      public FileDemo(String name, int age)
      {
          this.name = name;
          this.age = age;
      }
      public void setName(String name)
      {
          this.name = name;
      }
      public void setAge(int age)
      {
          this.age = age;
      }
      public void setFatherName(String fatherName)
      {
          this.fatherName = fatherName;
      }
      override
      public String ToString ()
      {
          return 'Name : ' + this.name + ' Age : ' + this.age+' Father Name : '+this.fatherName;
      }
  }
}
Output –
What is Delegate in C# with example?

Multicast delegate example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1 {
    // create the delegate in namespace
   public delegate void MyDelName(String name);
   public delegate void MyDelegateAge(int age );
    //creating the program class in namespace
   public class Program {
      static void Main(string[] args)
      {
          // create the object of class FileDemo
          FileDemo fDemo = new FileDemo();
          // before apply the delegation print the fDemo onject as string
          Console.WriteLine(fDemo +'\n' );
          // create the first delegate object for storing the setName method as argument in delegation
          MyDelName setNameDel = new MyDelName(fDemo.setName);
          // multi cast of delegattion adding of delegate
          setNameDel += new MyDelName(fDemo.setFatherName);
          setNameDel('Shivam Prajapati');
          // and second delegation object for string the setAge method as argument in delegation
          MyDelegateAge setNameAge = new MyDelegateAge(fDemo.setAge);
          setNameAge(23);
          // after apply the delegation print the fDemo onject as string
          Console.WriteLine(fDemo + '\n');
          // multi cast of deleation subtracting of delegate
          setNameDel -= new MyDelName(fDemo.setFatherName); // remove the setFather method
          setNameDel('Ravi');
          // after subtract the delegate
          Console.WriteLine(fDemo + '\n');
          Console.ReadKey();
      }
  }
    //craeting the file demo class in namepace
  class FileDemo
  {
      // fields
      private String name;
      private String fatherName;
      private int age;
      // methods
      public FileDemo()
      {
          this.name = 'name';
          this.age = 0;
          this.fatherName = 'father name';
      }
      public FileDemo(String name, int age)
      {
          this.name = name;
          this.age = age;
      }
      public void setName(String name)
      {
          this.name = name;
      }
      public void setAge(int age)
      {
          this.age = age;
      }
      public void setFatherName(String fatherName)
      {
          this.fatherName = fatherName;
      }
      override
      public String ToString ()
      {
          return 'Name : ' + this.name + ' Age : ' + this.age+' Father Name : '+this.fatherName;
      }
  }
}
Output –

What is Delegate in C# with example?