[C#] Delegate vs Action vs Func

Devartrio
3 min readNov 21, 2024

--

In C#, there are three commonly used types for handling events and callbacks: delegate, Action, and Func. Each type has its own advantages depending on the situation, and understanding how to use them appropriately is essential. In this post, we will summarize the differences between delegate, Action, and Func, along with their pros and cons.

What is Delegate?

Delegate is the most fundamental delegate type in C#, which acts as a reference to methods. It allows methods to be passed as parameters or multiple methods to be chained together. Delegates are useful for dynamically calling methods that meet specific conditions.

Example

public delegate void PrintDelegate(string message);

public class DelegateExample
{
public static void PrintMessage(string message)
{
Console.WriteLine(message);
}

public static void Main()
{
PrintDelegate print = PrintMessage;
print("Hello, Delegate!");
}
}

Pros

1. Flexible Method Linking : Delegates can link multiple methods and call them at once. This is called multicast delegation and is useful when you need to execute several methods in sequence.
2. Custom Type Creation : Delegates can be defined as new types, allowing you to create a type that references methods with a specific signature.

Cons

1. Verbose Declaration : Delegates require a separate type declaration, which can make the code longer and less concise compared to Action or Func.
2. Complex Type Definition : Defining and using delegate types can be cumbersome when you only need to connect simple methods.

What is Action?

Action is a delegate type that can reference methods without a return value. Depending on the number of parameters, different types of Actions exist, and it can take up to 16 parameters. Action is a standardized type that helps make using delegates more concise.

Example

public class ActionExample
{
public static void Main()
{
Action<string> print = message => Console.WriteLine(message);
print("Hello, Action!");
}
}

Pros

1. Concise Usage : Action does not require a separate declaration, which makes the code more concise. Methods can be referenced directly, or lambda expressions can be used to make the code even simpler.
2. Easy Callback Handling : When dealing with events or callbacks, Action makes the implementation very intuitive and short.

Cons

1. No Return Value : Since Action cannot return a value, it is not suitable when you need to return the result of a particular operation.

What is Func?

Func is a delegate type that references methods with return values. Func can take between 0 and 16 parameters, and the last type parameter represents the return type. This makes Func useful for referencing methods that return a result.

Example

public class FuncExample
{
public static void Main()
{
Func<int, int, int> add = (a, b) => a + b;
int result = add(3, 4);
Console.WriteLine($"Result: {result}");
}
}

Pros

1. Handling Return Values : Func is suitable when the result of a method is needed. It allows for concise logic when working with the result of an operation.
2. Standardized Usage : Like Action, Func is easy to use without defining a delegate explicitly.

Cons

1. Limited Use Case : If a return value is not needed, Func may feel unnecessary compared to Action.

When to Use What?

- When you need basic events and multicast capabilities : Use Delegate. It is especially useful for event handling when you need to link multiple methods at once.
- When you need to handle simple callbacks without return values : Use Action. It is concise and intuitive, making it suitable for simple callbacks and event handling.
- When you need to handle tasks that require return values : Use Func. It is useful when the result of an operation needs to be returned clearly.

Each of these three types is beneficial in different scenarios and plays a vital role in improving code readability and maintainability. Delegate is advantageous in terms of flexibility and extensibility, allowing you to dynamically link and invoke multiple methods. Action helps write concise and intuitive code, making it well-suited for handling callbacks or simple events. Func is ideal for handling return values and is suitable when you need to return and utilize the result of a specific task. Therefore, understanding the characteristics of each type and selecting appropriately based on your project’s requirements is crucial. Delegates offer flexibility and extensibility, while Action and Func provide simplicity and ease of use, making it important to choose wisely depending on the situation.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Devartrio
Devartrio

Written by Devartrio

Game Developer from South Korea.

No responses yet

Write a response