5,699,997 members and growing! (15,125 online)
Email Password   helpLost your password?
Languages » C# » Delegates and Events     Intermediate License: The Code Project Open License (CPOL)

Fun with Func

By abhigad

C# Generic Delegate
C#, .NET (.NET 3.5, .NET), Dev

Posted: 7 Jul 2008
Updated: 7 Jul 2008
Views: 2,707
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.45 Rating: 2.08 out of 5
2 votes, 40.0%
1
0 votes, 0.0%
2
3 votes, 60.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5

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


MS Computer Science + Information Science

Blog: SOAAS

abhijit gadkari
abhijit dot gadkari at gmail dot com
Santa Ana,CA-92705
Occupation: Architect
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralFunc<int,> testFuncWithLambda = inNum => (inNum > 3);memberexistenz_22:05 7 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jul 2008
Editor: Deeksha Shenoy
Copyright 2008 by abhigad
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project