Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C#
Article

Fun with Func

Rate me:
Please Sign up or sign in to vote.
2.53/5 (10 votes)
7 Jul 2008CPOL2 min read 70.8K   262   30   4
C# Generic Delegate

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

C#
delegate bool IsGreaterThan(int inputNumber);

Step 2

C#
static bool IsGreaterThan3(int number)
{
return number > 3 ? true : false;
}

Step 3

C#
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:

C#
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:

C#
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:

C#
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:

C#
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:

C#
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:

C#
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)


Written By
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
Irvine-92618,CA

Comments and Discussions

 
GeneralMy vote of 5 Pin
M Rayhan18-Nov-13 21:27
M Rayhan18-Nov-13 21:27 
GeneralMy vote of 3 Pin
sunder.tinwar28-Aug-13 7:46
professionalsunder.tinwar28-Aug-13 7:46 
GeneralMy vote of 3 Pin
Nitish Kakkar20-Jun-13 23:48
Nitish Kakkar20-Jun-13 23:48 
GeneralFunc&lt;int,&gt; testFuncWithLambda = inNum =&gt; (inNum &gt; 3); Pin
existenz_7-Jul-08 21:05
existenz_7-Jul-08 21: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.