C#
method group
programming
.NET
coding basics

What is a method group in C#?

Codemia Knowledge Hub ·

A method group in C# is the name of a method referenced without parentheses. Writing Console.WriteLine instead of Console.WriteLine("hello") refers to the method group: the whole set of overloads that share that name. The compiler uses the surrounding context (a delegate type, a lambda target, or a LINQ method parameter) to pick the specific overload.

Method groups sit behind delegates, event subscriptions and LINQ, and knowing what they are explains a whole family of compiler errors.

A method group is not a value on its own

You cannot store a method group in a variable directly. It is an expression the compiler resolves to one specific method once it has enough context, and that context is almost always a delegate signature.

Consider a class with overloads:

csharp
1public class MathOps
2{
3    public static int Add(int a, int b) => a + b;
4    public static double Add(double a, double b) => a + b;
5    public static string Add(string a, string b) => a + b;
6}

MathOps.Add is a method group with three overloads. Assigning it to var fails because the compiler cannot tell which overload you mean, but assigning it to a delegate type works:

csharp
Func<int, int, int> intAdd = MathOps.Add;     // picks Add(int, int)
Func<double, double, double> dblAdd = MathOps.Add; // picks Add(double, double)

Method group conversion

Method group conversion is the implicit step in which the compiler turns a method group into a delegate. It follows normal overload resolution, except the "arguments" are the delegate's parameter types rather than actual values.

csharp
1public delegate int BinaryOp(int x, int y);
2
3BinaryOp op = MathOps.Add;  // method group conversion to BinaryOp
4int result = op(5, 3);       // invokes Add(int, int), returns 8

The conversion fails at compile time if no overload matches the delegate signature, or if more than one matches ambiguously. It checks the return type as well as the parameters: Func<int> will not accept a void method even if the parameters match.

Method groups vs. lambda expressions

Before C# 2.0 added anonymous methods and C# 3.0 added lambdas, method groups were the main way to create delegate instances. Today both styles are common:

csharp
1// Method group syntax
2button.Click += HandleClick;
3
4// Lambda syntax
5button.Click += (sender, e) => HandleClick(sender, e);

Both behave the same. The method group form is shorter and can avoid an extra allocation: the compiler can sometimes cache a delegate created from a method group, while a lambda that captures variables always allocates.

ScenarioRecommendedReason
Event subscription to an existing methodMethod groupShorter, no wrapper delegate
Inline logic with no existing methodLambdaNo need to define a named method
LINQ projection with a simple static methodMethod grouplist.Select(int.Parse) reads cleanly
Logic that captures local variablesLambdaMethod groups cannot capture locals
Unsubscribing from eventsMethod groupSame method reference enables -= removal

Method groups on instance methods do capture this, but they cannot capture local variables. If you need a closure, use a lambda.

Method groups in LINQ

Method groups read well in LINQ when an existing static method already does the job:

csharp
1var numbers = new[] { "1", "2", "3", "not-a-number", "5" };
2
3// Lambda style
4var parsedA = numbers.Select(s => int.Parse(s));
5
6// Method group style
7var parsedB = numbers.Select(int.Parse);

The two lines are equivalent; the method group version signals that nothing happens beyond int.Parse.

Method groups only fit when the method signature matches the delegate parameter exactly. If you need to negate, transform or add arguments, you need a lambda:

csharp
1var lines = File.ReadAllLines("data.txt");
2
3// Filter blank lines using a method group
4var blanks = lines.Where(string.IsNullOrWhiteSpace).ToList();  // wrong: this keeps blanks
5var nonEmpty = lines.Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); // correct
6
7// An instance method with no arguments still needs a lambda
8var trimmed = lines.Select(s => s.Trim()); // lambda needed for instance method with no args

Subscribing and unsubscribing events

Event subscription is one of the most common uses:

csharp
1public class OrderService
2{
3    public event EventHandler<OrderEventArgs> OrderPlaced;
4
5    public void PlaceOrder(Order order)
6    {
7        // process order
8        OrderPlaced?.Invoke(this, new OrderEventArgs(order));
9    }
10}
11
12public class NotificationService
13{
14    public void Subscribe(OrderService orderService)
15    {
16        orderService.OrderPlaced += OnOrderPlaced;  // method group
17    }
18
19    public void Unsubscribe(OrderService orderService)
20    {
21        orderService.OrderPlaced -= OnOrderPlaced;  // same method group for removal
22    }
23
24    private void OnOrderPlaced(object sender, OrderEventArgs e)
25    {
26        Console.WriteLine($"Order {e.Order.Id} placed");
27    }
28}

Using the method group for both += and -= means the delegates match, so unsubscribing works. Subscribing with event += (s, e) => Handle(s, e) and later writing event -= (s, e) => Handle(s, e) does not remove anything, because the two lambdas are different delegate instances. Use a method group or keep the delegate in a field.

The "Cannot convert method group to non-delegate type" error

This error appears when you write a method name without parentheses where a value is expected:

csharp
1// Error CS0428: Cannot convert method group 'ToString' to non-delegate type 'string'
2string s = myObject.ToString;
3
4// Fix: add parentheses to invoke the method
5string text = myObject.ToString();

You referenced the method group when you meant to call the method, and the fix is almost always adding (). The term "method group" in the message is what confuses people who have not met it before.

The reverse mistake is adding parentheses where a method group is expected:

csharp
1// Error: cannot assign invocation result to delegate
2Func<string> getter = myObject.ToString();  // wrong: this calls ToString and tries to assign the string
3
4// Fix: remove parentheses to pass the method group
5Func<string> getterFixed = myObject.ToString;    // correct: assigns the method group

When several overloads could match a delegate type, the compiler reports an ambiguity error. Resolve it with a cast or a lambda that selects the overload explicitly.

Natural type for method groups (C# 10+)

Since C# 10, a method group can have a "natural type", so the compiler can infer a delegate type for var without you naming one. This only works when the group has a single candidate method. A method with one overload, such as Console.Read, gets a natural type. Console.WriteLine has many overloads, so it gets none and the second line below does not compile.

csharp
var read = Console.Read;         // inferred as Func<int>
var action = Console.WriteLine;  // error CS8917: the delegate type could not be inferred

Before natural types, any such assignment failed with "cannot assign method group to implicitly typed variable."

Method groups, delegates and lambdas side by side

ConceptWhat It IsExample
Method groupA reference to one or more overloads by nameConsole.WriteLine
DelegateA type-safe function pointer typeAction<string>, Func<int, bool>
Delegate instanceAn object pointing to a specific methodnew Action<string>(Console.WriteLine)
Lambda expressionAn anonymous function defined inlinex => x > 0
Method group conversionImplicit conversion from method group to delegateAction<string> a = Console.WriteLine;

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms