Learn Delegates In C#

Delegate in C# is a type that allows you to pass a method as a parameter and get a return value. Delegates are often used to deal with events, The C# delegate type in .NET represents a delegate. This article teaches you, how to use and implement delegates in C#.

A delegate is a type-safe object that can point to another method (or possibly multiple methods) in the application, which can be invoked at a later time. A delegate type is defined using the following three parts. 

  1. The name of the method on which a delegate calls.
  2. Any argument (if any) of a method.
  3. The return value (if any) of a method. 

Defining a Delegate in C#

When you want to create a delegate in C#, you make use of the delegate keyword.

The name of your delegate can be whatever you desire. However, you must define the delegate to match the signature of the method it will point to. for example, the following delegate can point to any method taking two integers and returning an integer. 

public delegate int DelegateName(int x, int y);

A Delegate Usage Example 

namespace MyFirstDelegate {  
    //This delegate can point to any method,  
    //taking two integers and returning an  
    //integer.  
    public delegate int MyDelegate(int x, int y);  
    //This class contains methods that MyDelegate will point to.  
    public class MyClass {  
        public static int Add(int x, int y) {  
            return x + y;  
        }  
        public static int Multiply(int x, int y) {  
            return x * y;  
        }  
    }  
    class Program {  
        static void Main(string[] args) {  
            //Create an Instance of MyDelegate  
            //that points to MyClass.Add().  
            MyDelegate del1 = new MyDelegate(MyClass.Add);  
            //Invoke Add() method using the delegate.  
            int addResult = del1(5, 5);  
            Console.WriteLine("5 + 5 = {0}\n", addResult);  
            //Create an Instance of MyDelegate  
            //that points to MyClass.Multiply().  
            MyDelegate del2 = new MyDelegate(MyClass.Multiply);  
            //Invoke Multiply() method using the delegate.  
            int multiplyResult = del2(5, 5);  
            Console.WriteLine("5 X 5 = {0}", multiplyResult);  
            Console.ReadLine();  
        }  
    }  
}

Delegate ability to Multicast

Delegate's ability to multicast means that a delegate object can maintain a list of methods to call, rather than a single method if you want to add a method to the invocation list of a delegate object, you make use of the overloaded += operator, and if you want to remove a method from the invocation list you make use of the overloaded operator -=.

Note

The Multicast delegate here contains methods that return void if you want to create a multicast delegate with a return type, you will get the return type of the last method in the invocation list. 

A Multicast Delegate Example

namespace MyMulticastDelegate {  
    //this delegate will be used to call more than one  
    //method at once  
    public delegate void MulticastDelegate(int x, int y);  
    //This class contains methods that MyDelegate will point to.  
    public class MyClass {  
        public static void Add(int x, int y) {  
            Console.WriteLine("You are in Add() Method");  
            Console.WriteLine("{0} + {1} = {2}\n", x, y, x + y);  
        }  
        public static void Multiply(int x, int y) {  
            Console.WriteLine("You are in Multiply() Method");  
            Console.WriteLine("{0} X {1} = {2}", x, y, x * y);  
        }  
    }  
    class Program {  
        static void Main(string[] args) {  
            //Create an Instance of MulticastDelegate  
            //that points to MyClass.Add().  
            MulticastDelegate del = new MulticastDelegate(MyClass.Add);  
            //using the same instance of MulticastDelegate  
            //to call MyClass.Multibly() by adding it to it's  
            //invocation list.  
            del += new MulticastDelegate(MyClass.Multiply);  
            //Invoke Add() and  Multiply() methods using the delegate.  
            //Note that these methods must have a void return vlue  
            Console.WriteLine("****calling Add() and Multibly() Methods.****\n\n");  
            del(5, 5);  
            //removing the Add() method from the invocation list  
            del -= new MulticastDelegate(MyClass.Add);  
            Console.WriteLine("\n\n****Add() Method removed.****\n\n");  
            //this will invoke the Multibly() method only.  
            del(5, 5);  
        }  
    }  
}

Delegate Covariance

Assume you are designing a delegate that can point to methods returning a custom class type, 

//Define a delegate pointing to methods returning Employee types.  
public delegate Employee EmployeeDelegate(); 

if you were to derive a new class from Employee Type named SalesEmployee and wish to create a delegate type that can point to methods returning this class type, you would be required to define an entirely new delegate to do so

//a new  delegate pointing to methods returning SalesEmployee types.  
public delegate SalesEmployee SalesEmployeeDelegate(); 

Example

namespace MyEmployeesDelegate {  
    //Define a delegate pointing to methods returning Employee types.  
    public delegate Employee EmployeeDelegate();  
    //a new  delegate pointing to methods returning SalesEmployee types.  
    public delegate SalesEmployee SalesEmployeeDelegate();  
    class Program {  
        public static Employee GetEmployee() {  
            return new Employee();  
        }  
        public static SalesEmployee GetSalesEmployee() {  
            return new SalesEmployee();  
        }  
        static void Main(string[] args) {  
            EmployeeDelegate empDel = new EmployeeDelegate(GetEmployee);  
            Employee emp = empDel();  
            SalesEmployeeDelegate salesEmpDel = new SalesEmployeeDelegate(GetSalesEmployee);  
            SalesEmployee emp2 = salesEmpDel();  
        }  
    }  
    public class Employee {  
        protected string firstName;  
        protected string lastName;  
        protected int Age;  
        public Employee() {}  
        public Employee(string fName, string lName, int age) {  
            this.firstName = fName;  
            this.lastName = lName;  
            this.Age = age;  
        }  
    }  
    public class SalesEmployee: Employee {  
        protected int salesNumber;  
        public SalesEmployee() {}  
        public SalesEmployee(string fName, string lName, int age, int sNumber): base(fName, lName, age) {  
            this.salesNumber = sNumber;  
        }  
    }  
}

It would be ideal to build a single delegate type that can point to methods returning either Employee or SelesEmployee types.

Covariance allows you to build a single delegate that can point to methods returning class types related by classical inheritance.

Delegate Covariance Example

namespace DelegateCovariance {  
    //Define a single delegate that may return an Employee  
    // or SalesEmployee  
    public delegate Employee EmployeeDelegate();  
    class Program {  
        public static Employee GetEmployee() {  
            return new Employee();  
        }  
        public static SalesEmployee GetSalesEmployee() {  
            return new SalesEmployee();  
        }  
        static void Main(string[] args) {  
            EmployeeDelegate emp = new EmployeeDelegate(GetEmployee);  
            Employee emp1 = emp();  
            EmployeeDelegate empB = new EmployeeDelegate(GetSalesEmployee);  
            //to obtain a derived type you must perform an explicit cast.  
            SalesEmployee emp2 = (SalesEmployee) empB();  
        }  
    }  
    public class Employee {  
        protected string firstName;  
        protected string lastName;  
        protected int Age;  
        public Employee() {}  
        public Employee(string fName, string lName, int age) {  
            this.firstName = fName;  
            this.lastName = lName;  
            this.Age = age;  
        }  
    }  
    public class SalesEmployee: Employee {  
        protected int salesNumber;  
        public SalesEmployee() {}  
        public SalesEmployee(string fName, string lName, int age, int sNumber): base(fName, lName, age) {  
            this.salesNumber = sNumber;  
        }  
    }  
}

I hope you now have a good idea about how to create and use delegate types in C# and .NET.

Here is a detailed article on delegates and events in C# and .NET: Mastering Delegates and Events In C# .NET.

 


Similar Articles