DEV Community

Ravi Sankar Rao
Ravi Sankar Rao

Posted on

C# Delegates Simplified — Part 1

This article is the first of a series of 3 articles on C# delegates.

Example we will be working with

Before directly jumping to delegates, lets take a look at the example. We have a static Maths class with two functions — FindSquare and FindSqrRoot. static here does not signify anything. It’s used just to simplify the course of this article.

public static class Maths
{
        public static double FindSquare(double number)
        {
            return number * number;
        }
        public static double FindSqrRoot(double number)
        {
            return Math.Sqrt(number);
        }
}
Enter fullscreen mode Exit fullscreen mode

Both functions take a single parameter of type double, and both return double. Note that the signature of both methods is same because this will come into picture later.

We invoke these methods as per our need.

double number = 25;

Maths.FindSqrRoot(number);
Maths.FindSquare(number);
Enter fullscreen mode Exit fullscreen mode

Coming to Delegates

Definition

If we go by the definition given in C# docs, A delegate is a type that represents references to methods with a particular parameter list and return type. To simplify it — a delegate is a type that defines a method signature.

How do we declare a delegate?

It is declared almost similar to a function except that it does not has a statement body for itself. It has a return type and might take parameters if required exactly like functions. Below is an example.

// No return type, No parameters
public delegate void MyDelegate();

// Has a return type, takes parameters
public delegate int MyDelegate(int someValue);
Enter fullscreen mode Exit fullscreen mode

In our example, for the two methods if we declare a delegate, it will be as below.

public delegate double CalculationsWithSingleNumber(double number);
Enter fullscreen mode Exit fullscreen mode

If you compare this with our two Maths functions, you will notice the signature is exactly the same, i.e. the return type and parameters are same.

Using a Delegate

Using a delegate is very simple too. We declare a variable of our delegate type and assign a function reference as it’s value. The delegate variable acts now as a function itself. We can invoke it just like a function, send our number and get the required result.

CalculationsWithSingleNumber doSomeMath = Maths.FindSquare;
double result = doSomeMath(10); // returns the square of the given number

doSomeMath = Maths.FindSqrRoot;
result = doSomeMath(25); // returns the square root of the given number
Enter fullscreen mode Exit fullscreen mode

We can assign a function to a delegate which has the exact same signature.
Below is an example of function with two parameters. Trying to assign it to our delegate will result in compilation error with message saying — No overload for matches delegate

public static double AddNumbers(double a, double b)
{
     return a + b;
}

// This cannot be done since AddNumbers takes 2 parameters and the delegate takes only 1
CalculationsWithSingleNumber doSomeMoreMath = Maths.AddNumbers;
Enter fullscreen mode Exit fullscreen mode

We saw how a delegate is declared, assigned and invoked, but one question that comes to mind is how we significantly use delegates in our programming.

Please visit article part 2 of this series for a simple example and implementation.

Top comments (0)