Click here to Skip to main content
Click here to Skip to main content

Fun with Func

By , 7 Jul 2008
Rate this:
Please Sign up or sign in to vote.

Introduction

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));
Console.WriteLine(ie.ToString());
Console.WriteLine(ExpressionFunc.Body);
Console.WriteLine(ExpressionFunc.Body.NodeType);
foreach (var paramname in ExpressionFunc.Parameters)
{
    Console.WriteLine("parameter name is {0}", paramname.Name);
}
Console.WriteLine(ExpressionFunc.Type);

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!

History

  • 7th July, 2008: Initial post

License

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

About the Author

abhigad
Architect ZimbaTech Solutions
United States United States
MS Computer Science + Information Science
Consulting + Software Development
Blog: Profile

Company: ZimbaTech Solutions
abhijit gadkari
abhijit dot gadkari at gmail dot com
Santa Ana,CA-92705

Comments and Discussions

 
GeneralMy vote of 5 PinmemberM Rayhan18-Nov-13 21:27 
GeneralMy vote of 3 Pinprofessionalsunder.tinwar28-Aug-13 7:46 
GeneralMy vote of 3 PinmemberNitish Kakkar20-Jun-13 23:48 
GeneralFunc&lt;int,&gt; testFuncWithLambda = inNum =&gt; (inNum &gt; 3); Pinmemberexistenz_7-Jul-08 21:05 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 7 Jul 2008
Article Copyright 2008 by abhigad
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid