C# Programming
Method Passing
Parameters
Software Development
Coding Techniques

Pass Method as Parameter using C#

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Passing a method as a parameter in C# means passing a delegate, a lambda expression, or a method group that matches a delegate type. This is one of the core techniques behind callbacks, strategy selection, LINQ-style APIs, and many event-driven designs.

The Core Idea: Delegates

A delegate is a type-safe reference to a method. If a method parameter expects a delegate, any compatible method can be supplied.

csharp
1using System;
2
3class Program
4{
5    static void PrintMessage(string message)
6    {
7        Console.WriteLine(message);
8    }
9
10    static void Execute(Action<string> action)
11    {
12        action("Hello from the callback");
13    }
14
15    static void Main()
16    {
17        Execute(PrintMessage);
18    }
19}

Here, Execute accepts an Action<string>, and PrintMessage is passed in as the method parameter.

Action And Func Are Usually Enough

You do not always need a custom delegate type. C# already provides:

  • 'Action for methods that return void'
  • 'Func for methods that return a value'
  • 'Predicate<T> for boolean checks on one argument'
csharp
1using System;
2
3class Program
4{
5    static int Add(int a, int b) => a + b;
6
7    static int Calculate(int x, int y, Func<int, int, int> operation)
8    {
9        return operation(x, y);
10    }
11
12    static void Main()
13    {
14        int result = Calculate(3, 4, Add);
15        Console.WriteLine(result);
16    }
17}

This pattern is common when you want the caller to choose the behavior.

Lambdas Work Too

Instead of passing a named method, you can pass a lambda directly.

csharp
1using System;
2
3class Program
4{
5    static void Repeat(int count, Action<int> action)
6    {
7        for (int i = 0; i < count; i++)
8        {
9            action(i);
10        }
11    }
12
13    static void Main()
14    {
15        Repeat(3, i => Console.WriteLine($"Iteration {i}"));
16    }
17}

Lambdas are often the cleanest choice when the callback is short and used only once.

Custom Delegate Example

If the meaning of the callback deserves a domain-specific name, define a custom delegate.

csharp
1using System;
2
3public delegate bool Rule(int value);
4
5class Program
6{
7    static void CheckValue(int value, Rule rule)
8    {
9        Console.WriteLine(rule(value));
10    }
11
12    static bool IsEven(int x) => x % 2 == 0;
13
14    static void Main()
15    {
16        CheckValue(10, IsEven);
17    }
18}

This is more descriptive than a raw Func<int, bool> when the delegate has a clear role in the API.

Why This Is Useful

Passing methods as parameters is especially useful for:

  • callbacks after work completes
  • sorting or filtering logic chosen by the caller
  • retry or validation policies
  • replacing large switch or if chains with injected behavior

It also supports separation of concerns. One method manages the workflow, while the caller supplies the behavior.

Method Groups Versus Invocation

One common beginner mistake is writing Execute(PrintMessage()). That calls the method immediately and passes its result, which is not what you want. To pass the method itself, use Execute(PrintMessage).

That difference is small syntactically but fundamental semantically.

Common Pitfalls

The most common mistake is passing the result of a method call instead of the method group itself.

Another mistake is choosing a delegate signature that does not match the target method. The parameter types and return type must line up.

A third issue is overusing custom delegates when Action or Func would already express the intent clearly and keep the API simpler.

Summary

  • In C#, methods are passed as delegates.
  • 'Action and Func cover most common callback signatures.'
  • Named methods, method groups, and lambdas can all be passed as parameters.
  • This pattern is useful for callbacks, strategy selection, and reusable workflow code.
  • Pass the method reference, not the result of invoking the method.

Course illustration
Course illustration

All Rights Reserved.