#1,151 – The Func Delegate Type
August 1, 2014 1 Comment
The .NET Framework includes the Func predefined delegate type, which represents the signature of a method that returns some type and accepts zero or more parameters.
Overloads of Func are :
- delegate TResult Func<out TResult>()
- delegate TResult Func<in T, out TResult>(T p1)
- delegate TResult Func<in T1, in T2, out TResutl>(T1 p1, T2 p2)
- etc…
You might define your own delegate type:
public delegate T Merger<T>(T thing1, T thing2); static void MergeSomething<T>(Merger<T> someMerger, T thing1, T thing2) { T result = someMerger(thing1, thing2); }
However, you could instead use the Func delegate type, avoiding the definition of your own custom delegate type.
static void MergeSomething2<T>(Func<T, T, T> someMerger, T thing1, T thing2) { T result = someMerger(thing1, thing2); }
Func<T,T,T> indicates that we’ll pass in a function that takes two parameters of a given type and returns a value of the same type.