Using Delegates in C# – C# Programming Example
C# Class

Using Delegates in C#

This article discusses an important topic about delegated types in C # (csharp).

It will undoubtedly be part of a series of articles on the basics of CSharp.

So we’ll see what delegates are and why they’re so important in .NET application development.

What is a delegate type?

In fact, a delegate type is a very simple concept: it is a type that allows you to reference a method of a class.
Thanks to delegates, you can pass a method as parameter (it’s a bit like the function pointers in C / C ++ for example).

A delegate type is always associated with the signature of a method.
Only methods with the same signature can be used with this delegate.

delegate is also a keyword in the C # language (csharp) that allows you to declare a delegate.

To declare a delegate in CSharp, here’s what to do:

  • In your class, you use the delegate keyword followed by the signature of the associated method,
  • The name of the method indicated in the signature will be the name of your delegate,
  • You can optionally add a visibility indicator at the beginning (private, public or protected for example).

Here’s an example code that allows you to declare a delegate in C #:

In this example, I declare a delegate for a method that takes an int list and returns an int.
All the methods corresponding to this signature can be used with this delegate.

 

How to use a delegate?

After declaring a delegate type, you must now use your delegate in your CSharp code.

A delegate type can be used like any type in C #: in methods (input arguments, return parameter) or in classes (property or member).

Here is an example of a delegate type usage in C # (following the previous example):

Ok, so far, you’ll tell me: well, but what’s the use of a delegate?

The most common use of delegate types

C # delegate types are used to pass methods as arguments during calls.
It is therefore possible to create different behaviors for the same code.

Indeed, delegate types are extension points of your code.
It will be possible to change the behavior of a piece of code simply by passing another delegate.

Here is a very simple example:

  • You have a class that performs calculations on a series of numbers,
  • A “Calculate” method takes as argument a list of integers and two delegates: BeforeProcessingDelegate and AfterDelegationDelegate,
  • These two delegates have the function of being able to filter or modify a series of elements before or after a treatment,
  • The Calculate method can be called several times in your application with references to different methods.

To better understand, I propose an example that shows two different calls:

 

As you can see, it is possible to do different operations depending on the methods passed in parameter.

  • In the first case, we call Calculate without a method of treatment.
  • In the second case, we call Calculate with both methods of treatment, which will produce a completely different result.

 

Why are delegates so important?

Delegate types are important in C # for two main reasons:

First, because they are used everywhere in the Dotnet framework. It is therefore very important to understand this concept to use the .NET framework correctly.

How to go further ?

If you want to use the .NET Framework optimally, I strongly advise you to practice and try to use and then create your own delegates.

That’s why I have prepared, as a bonus for this article, a method sheet that summarizes the essentials and offers exercises to go further in practice.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: