Delegates in C#

Introduction

We have all learned how to implement reference types using language constructs like.

  • Classes
  • References

These reference types allow programmers to create instances of objects and use them in special ways for development procedure purposes.

Classes allow us to create objects that contain members with attributes or functionality. On the other hand, the interface allows us to declare a set of attributes and behaviors that all objects are implementing for exposing them publicly or privately.

Now I am introducing a new reference type, & delegate".

A delegate is a C# language element that allows programmers to reference a method for their development work. Generally, C or C++ programmers are much more familiar with the concept of delegates in comparison to others.

C# Delegates Features

  • Maximum Flexibility.
  • Several built-in functionalities at runtime.
  • Manipulation of variables.

Delegate Features

C# Delegates Description

Delegates provide flexibility and scalability so that it can be used consistent with the requirements or goals of your development procedure. There are fewer boundary limitations with delegates. Interfaces also can be used instead of delegates but due to some extra functionality programmers do prefer delegates instead of other reference methods.

C# Delegates Declaration

Declaration of delegates is somewhat similar to methods, except that a delegate has the extra feature of a delegate modifier. A delegate modifier is terminated with a semicolon (;) and requires no implementation.

Example

public delegate int Comparer(object obj1, object obj2);

C# Delegates Delegate Handler

A delegate handler method works as follows.

public static int CompareFirstNames(object name1, object name2)
{
    // Your code logic here
    // ...
    // Ensure to return an integer value
}

Example

using System;
public delegate int Comparer(object obj1, object obj2);
public class Name
{
    public string FirstName = null;
    public string LastName = null;
    public Name(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }
    public static int CompareFirstNames(object name1, object name2)
    {
        string n1 = ((Name)name1).FirstName;
        string n2 = ((Name)name2).FirstName;
        return string.Compare(n1, n2);
    }
    public override string ToString()
    {
        return FirstName + " " + LastName;
    }
}
class SimpleDelegate
{
    Name[] names = new Name[6];
    public SimpleDelegate()
    {
        names[0] = new Name("abhishek", "jaiswal");
        names[1] = new Name("ashish", "verma");
        names[2] = new Name("gopi", "chand");
        names[3] = new Name("hari", "prakash");
        names[4] = new Name("prashant", "kumar");
        names[5] = new Name("pratiyush", "anand");
    }
    static void Main(string[] args)
    {
        SimpleDelegate sd = new SimpleDelegate();
        Comparer cmp = new Comparer(Name.CompareFirstNames);

        Console.WriteLine("\nBefore Sorting: \n");
        sd.PrintNames();
        sd.Sort(cmp);
        Console.WriteLine("\nAfter Sorting: \n");
        sd.PrintNames();
    }
    public void Sort(Comparer compare)
    {
        object temp;
        for (int i = 0; i < names.Length; i++)
        {
            for (int j = i; j < names.Length; j++)
            {
                if (compare(names[i], names[j]) > 0)
                {
                    temp = names[i];
                    names[i] = names[j];
                    names[j] = (Name)temp;
                }
            }
        }
    }
    public void PrintNames()
    {
        Console.WriteLine("Names: \n");
        foreach (Name name in names)
        {
            Console.WriteLine(name.ToString());
        }
    }
}


Similar Articles