C# Method Group Conversion to Delegate

Summary: in this tutorial, you’ll learn about C# method group and how the method group conversion to delegate process works.

What is a method group in C#

In C#, a method group is a set of overloaded methods that share the same name but have different parameter lists. When referring to a method by its name without specifying its parameters, you are referring to the method group.

For example, the following Calculator class has two Add methods with different parameter types:

class Calculator
{
    public static int Add(int a, int b) => a + b;
    public static double Add(double a, double b) => a + b;
}Code language: C# (cs)

When you refer to the Add method without specifying its arguments, you’re referring to the Add method group.

Method group conversion to delegate

In C#, a method group conversion to delegate is the process of converting a method group into a delegate type.

A delegate is a type that represents a reference to a method with a specified signature. By using a delegate, you can invoke a method or pass the method reference as an argument to other methods.

When you use a method group in a context that requires a delegate, the C# compiler automatically performs the method group conversion to delegate.

For example, you can assign a method group to a delegate variable like this:

class Calculator
{
    public static int Add(int a, int b) => a + b;
    public static double Add(double a, double b) => a + b;
}


class Program
{
    delegate int Calculate(int a, int b);

    public static void Main(string[] args)
    {
        Calculate cal = Calculator.Add;
        var result = cal(10, 20);

        Console.WriteLine(result);
    }
}Code language: C# (cs)

In this example, the Add method group is converted to the Calculate delegate type, which has the same signature as the Calculate method. This allows you to assign the method reference to a delegate variable cal, which can then be invoked with a string argument.

Note that the method group conversion to delegate is only allowed when the method group has a single best match for the delegate’s signature. If you have multiple methods in the group that could match the delegate signature, C# will issue an error.

Improved Method group conversion to delegate in C# 11

The following example demonstrates the improved method group conversion to delegate feature introduced in C# 11:

using static System.Console;

class Calculator
{
    public static int Add(int a, int b) => a + b;
    public static double Add(double a, double b) => a + b;
}

class Program
{
    static int Calculate(int x, int y, Func<int, int, int> f) => f(x, y);

    public static void Main(string[] args)
    {
        int x = 20, 
            y = 10;
        
        var result = Calculate(x, y, Calculator.Add);
        
        WriteLine(result); // 30
    }
}Code language: C# (cs)

Output:

30

In this example instead of passing a lambda expression to the Calculate() method:

var result = Calculate(x, y, (x,y) => Calculator.Add(x,y));Code language: PHP (php)

you can pass a method group to it:

var result = Calculate(x, y, Calculator.Add);Code language: JavaScript (javascript)

Behind the scenes, the C# compiler converts the method group to the delegate that accepts two integers and returns an integer.

Summary

  • A method group is a collection of methods that have the same name but different parameters.
  • Method group conversion to delegate is the process of automatically converting a method group into a delegate type that can be used to reference a method with a matching signature.
Was this tutorial helpful ?