FuncT with out parameter
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, Func<T> delegates cannot directly use out or ref parameters because the generic type parameter constraints do not support them. The Func<T1, TResult> signature expects regular value or reference types as parameters. To work around this, you can define a custom delegate, use tuple returns, or restructure your code to avoid out parameters entirely.
The Problem
Func<> generic type parameters do not support the out or ref modifiers.
Solution 1: Custom Delegate (Direct Fix)
Define your own delegate type with the out parameter:
Solution 2: Return a Tuple Instead
Wrap the out parameter method to return a tuple:
Solution 3: Use a Wrapper Class
Encapsulate the result in a class:
Solution 4: Use Nullable Return (C# 8+)
For value types, return null on failure:
This is the cleanest approach for TryParse-style methods on value types.
Why Func Does Not Support out/ref
The Func<> and Action<> delegates use generic type parameters:
The in/out here are variance modifiers (covariance/contravariance), not the out parameter modifier. C# generics cannot express out T or ref T as parameter types — this is a limitation of the CLR generic type system.
Practical Example: Dictionary TryGetValue
Passing to Higher-Order Functions
Common Pitfalls
- Confusing variance
outwith parameterout: InFunc<in T, out TResult>,outmeans covariant return type — completely different from theoutparameter modifier. - Lambda capture of out variables: You cannot capture an
outparameter in a lambda. The lambda must call the method internally and capture the result. - Performance: Wrapping in tuples or classes adds a small allocation overhead. For hot paths, use the custom delegate approach which has zero overhead.
- Async methods:
asyncmethods cannot haveoutparameters either. Return a tuple or result object instead:Task<(bool, T)>. - Default values: When using
int?return type, ensure callers handlenullproperly. Usevalue ?? defaultValueor pattern matching.
Summary
Func<T>cannot haveoutorrefparameters due to CLR generic limitations- Define a custom delegate (
delegate bool TryParse<T>(string s, out T result)) for direct support - Return a tuple
(bool success, T value)to wrapoutparameter methods intoFunc<> - Use
int?nullable returns forTryParse-style methods as the cleanest workaround - The variance
outinFunc<out TResult>is unrelated to the parameteroutmodifier
Related reading
- Fuzzy text sentences/titles matching in C
- Generate C class from XML
- Generate C classes from JSON Schema
- Generating a random decimal in C
- Generating a random, non-repeating sequence of all integers in .NET
- Generating Unique Numeric IDs using DateTime.Now.Ticks
- Generic List - moving an item within the list
- generic NOT constraint where T IEnumerable

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.