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, returnsint' - '
Func<int, int>means oneintparameter, returnsint' - '
Func<string, bool>means onestringparameter, returnsbool'
Here is a simple example:
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:
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.
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.
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
FuncwhereActionis 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
- '
Funcis 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.
- '
Actionis the sibling delegate for methods that return nothing.' - Use
Funcfor small, composable behaviors and prefer richer abstractions when the logic becomes domain-heavy.

