![]() |
Languages »
C# »
Delegates and Events
Intermediate
License: The Code Project Open License (CPOL)
Fun with FuncBy abhigadC# Generic Delegate |
C#, .NET (.NET 3.5), Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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:
delegate bool IsGreaterThan(int inputNumber);
static bool IsGreaterThan3(int number)
{
return number > 3 ? true : false;
}
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!
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Jul 2008 Editor: Deeksha Shenoy |
Copyright 2008 by abhigad Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |