Click here to Skip to main content
13,407,586 members (51,719 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 7 Jul 2008

Fun with Func

, 7 Jul 2008
Rate this:
Please Sign up or sign in to vote.
C# Generic Delegate


Fun with Func <T, TResult> : Generic Delegate

Func:generic delegate is a cool feature introduced with .NET 3.5. We will look at Func in this short article. But let's start with .NET 1.1.

In C# 1.1, we had code like the one shown below:

Step 1

delegate bool IsGreaterThan(int inputNumber);

Step 2

static bool IsGreaterThan3(int number)
return number > 3 ? true : false;

Step 3

IsGreaterThan igt = new IsGreaterThan(IsGreaterThan3);
Console.WriteLine("Number is greater than 3 : {0}", igt(4));

In step 1, we have a delegate definition. IsGreaterThan3 is a delegate target method. We can use this delegate as shown in step 3.

When C# 2.0 came out, this code was refactored as follows:

IsGreaterThan igt1 = delegate(int number1)
return number1 > 3 ? true : false;

Notice the anonymous method call.

And with C# 3.5, we got this new thing called Func<(T, TResult> Generic Delegate. As per the documentation – “[Func] Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter”. First, it looked like a keyword. But it is a .NET generic delegate.

In Func<int, bool>, integer is the input parameter and boolean value is the output parameter. All you need to use this generic delegate is to map it against the matching method call. For example:

Func<int, bool> testFunc = delegate(int number2)
return number2 > 3 ? true : false;
Console.WriteLine("Number is greater than 3 : {0}", testFunc(4));

Here, the anonymous method will take an integer as an input parameter and will return a boolean value based on the comparison result.

Notice: We did not define any delegate with this code. With Func, we got the delegate for free. And then with the lambda expression, same code can be refactored as follows:

Func<int, bool> testFuncWithLambda = inNum => inNum > 3 ? true : false;

Console.WriteLine("Number is greater than 3 : {0}", testFuncWithLambda(4));

One good example of Func can be found on this blog on C# in Depth.

We can use Func with multiple input variables of the same type. Look at the following example:

Func<int, int, int, int> ManyFunc = (int a, int b,int c) => a + b+ c;

Console.WriteLine("ManyFunc is {0}", ManyFunc(2, 2, 3));

If you use Reflector on this code, you will find code like the one shown below:

Func<int, int, int, int> ManyFunc = delegate (int a, int b, int c)
return (a + b) + c;

Console.WriteLine("ManyFunc is {0}", func123(2, 2, 3));

Also, Func plays an important role in the LINQ Expression Tree implementation. Expression tree can be used to convert the executable code into a data structure. Consider the following example:

Func<int, int, int, int> ManyFunc = (int a, int b,int c) => a + b+ c;
Console.WriteLine("ManyFunc is {0}", ManyFunc(2, 2, 3));
Expression<Func<int,int, int>> ExpressionFunc = (int a, int b) => a + b ;
InvocationExpression ie =
    Expression.Invoke(ExpressionFunc, Expression.Constant(4), Expression.Constant(5));
foreach (var paramname in ExpressionFunc.Parameters)
    Console.WriteLine("parameter name is {0}", paramname.Name);

In this example, we have a lambda expression as (int a, int b) => a + b, and an ExpressionFunc. ExpressionFunc is not data but an expression tree. We can find out the body, parameters, nodetype and type of the lambda expression using ExpressionFun.

I refactored some .NET 1.1 code with Func and lambda expression. Honestly, it was a lot of fun. Let me know if you find any other interesting Func test cases.

Have fun!


  • 7th July, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Wells Fargo
United States United States
MS Computer Science + Information Science
Architect at Wells Fargo [Wells Fargo Dealer Services -WFDS]
Blog: Profile

abhijit gadkari
abhijit dot gadkari at gmail dot com

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
M Rayhan18-Nov-13 22:27
memberM Rayhan18-Nov-13 22:27 
GeneralMy vote of 3 Pin
sunder.tinwar28-Aug-13 8:46
professionalsunder.tinwar28-Aug-13 8:46 
GeneralMy vote of 3 Pin
Nitish Kakkar21-Jun-13 0:48
memberNitish Kakkar21-Jun-13 0:48 
GeneralFunc&lt;int,&gt; testFuncWithLambda = inNum =&gt; (inNum &gt; 3); Pin
existenz_7-Jul-08 22:05
memberexistenz_7-Jul-08 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 7 Jul 2008
Article Copyright 2008 by abhigad
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid