Func
usage
programming
guide
tutorial

What is Func, how and when is it used

Master System Design with Codemia

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

Introduction

In C#, Func is a built-in generic delegate type for representing a method that returns a value. It is used whenever you want to pass executable behavior around as data, especially in LINQ, callbacks, and higher-order utility methods.

What Func Means

Func is shorthand for "a callable thing with parameters and a return value." The last generic type argument is always the return type.

Examples:

  • 'Func<int> means no parameters, returns int'
  • 'Func<int, int> means one int parameter, returns int'
  • 'Func<string, bool> means one string parameter, returns bool'

Here is a simple example:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        Func<int, int, int> add = (a, b) => a + b;
8        Console.WriteLine(add(2, 3));
9    }
10}

The variable add now stores a method-like value that can be invoked later.

Why Func Is Useful

Without delegates, you would need a separate named method for every bit of behavior you want to pass into another method. Func makes APIs more flexible because the caller can choose the logic.

For example, a helper can accept a transformation function:

csharp
1using System;
2
3class Program
4{
5    static int ApplyTwice(int value, Func<int, int> operation)
6    {
7        return operation(operation(value));
8    }
9
10    static void Main()
11    {
12        int result = ApplyTwice(3, x => x + 2);
13        Console.WriteLine(result);
14    }
15}

This prints 7 because the lambda runs twice.

Func in LINQ

A lot of LINQ works because methods such as Where and Select accept delegates. You usually write lambdas, but those lambdas are represented as Func values in many cases.

csharp
1using System;
2using System.Linq;
3
4class Program
5{
6    static void Main()
7    {
8        int[] numbers = { 1, 2, 3, 4, 5 };
9
10        var doubledEvens = numbers
11            .Where(n => n % 2 == 0)
12            .Select(n => n * 2);
13
14        foreach (int value in doubledEvens)
15        {
16            Console.WriteLine(value);
17        }
18    }
19}

The predicates and projections here are delegates. In query-heavy code, understanding Func helps explain what LINQ is really doing.

Func vs Action vs Custom Delegates

Use Func when there is a return value. Use Action when there is no return value.

csharp
Action<string> log = message => Console.WriteLine(message);
Func<int, int, int> multiply = (a, b) => a * b;

Custom delegate types are still useful when you want a meaningful domain name or attributes on the delegate type, but for many ordinary callback shapes, Func is enough.

When to Use It

Func is a good fit when:

  • you want to inject decision logic into a method
  • you are composing transformations
  • you are passing predicates, selectors, or calculators
  • you are writing reusable utilities that should not hard-code behavior

It is less useful when a full interface communicates intent better. If the behavior has state, multiple operations, or domain meaning, an interface or class is often clearer than passing several delegates around.

Common Pitfalls

  • Miscounting generic parameters. In Func<int, string, bool>, the first two types are parameters and the last one is the return type.
  • Using Func where Action is the correct delegate because nothing is returned.
  • Hiding important domain behavior behind vague delegate parameters. Sometimes an interface is clearer.
  • Forgetting that closures capture local variables, which can surprise you when a lambda runs later than expected.

Summary

  • 'Func is a built-in delegate type in C# for methods that return a value.'
  • The final generic type argument is the return type.
  • It is widely used in callbacks, helper methods, and LINQ.
  • 'Action is the sibling delegate for methods that return nothing.'
  • Use Func for small, composable behaviors and prefer richer abstractions when the logic becomes domain-heavy.

Course illustration
Course illustration

All Rights Reserved.