How Delegates Work in C#

Most developers new to C# don't fully understand how delegates work and it's a frequently asked question in the Productive C# membership. This post contains everything there is know about delegates in C#. Share this, if you think it can help an other fellow C# developer.

What is a C# delegate?

A C# delegate is a type that represent the signature of a .NET method.

A value of a delegate is effectively a method name.

A delegate is similar to a C++ function pointer but it is type-safe.

How to define a C# delegate?

To define a C# delegate, you use the delegate keyword followed by a method signature. This define a type that can reference any method that match the same signature.

How to define a C# delegate

Display defines a delegate type that can reference any method that take an integer and does not return.

Operation defines a delegate type that can reference any method that can take two integers and return an integer.

How to create an instance of a delegate?

You can create an instance of a delegate in the same way you create an instance of a type in C#. The value of a delegate is the name of the method you want to reference.

Create C# delegate instance explicitely

The type initialization can be even shorter as the C# compiler does not requires you to specify the type name of the delegate after the new operator.

Create C# delegate instance

It's also important to note that a delegate can point to a instance method.

Create C# delegate instance to instance method

How to invoke a delegate?

You can use a delegate the same way you call a method.

How to use a C# delegate

This method call an operation passing 5 and 3 as input and display the result.

The cool thing is that the method that a delegate reference can change at runtime. If you pass your delegate to a method, that method doesn't know what code is going to run when using the delegate.

This is effectively a way to implement polymorphism without creating a hierarchy of classes. This is a more functional and .NET idiomatic way to implement the strategy design pattern.

Why should I use a delegate?

A delegate allows you to implement the strategy design pattern in a more .NET idiomatic way. The code is short and compact and does not requires you to create interfaces and hierarchy of classes. Delegates provides a way to customize behavior at runtime.

Starting from C# 3, the language provides a very elegant way to define methods using lambdas expressions. This increase readability of your code significantly.

Lambda expressions as delegates

The .NET class libraries define generics delegates that can be used in all your applications: Action and Func. LINQ provides a vast collection of methods that accept delegates to perform many queries using a functional programming style. For an example see: SelectMany in LINQ.

Delegates can be used to implement callbacks and can point to a chain of methods.

Delegates are an underlying building blocks on which C# events are built upon.

Professional C# developers use delegates and lambda expressions every day at work. Understanding how delegate works is an essential skill for new C# developers.

Share the post and help your fellow C# developers to master the language!


Sharing is Caring
>