Monday, March 25, 2013

C# : Func Type

What is func?
Func<T> is a predefined delegate type for a method that returns some value of the type T

Visit below link for more details:
http://msdn.microsoft.com/en-gb/library/bb549151.aspx

Example

This program uses the lambda expression syntax with Func type instances to store anonymous methods. Each of the Func type instances uses type parameters, and these are specified in the angle brackets < and >.
Lambda
So: The first type parameters are the arguments to the methods, and the final type parameter is the return value.
Generic Class Return
Program that uses Func generic type and lambdas [C#]

using System;

class Program
{
    static void Main()
    {
 //
 // Create a Func instance that has one parameter and one return value.
 // ... Parameter is an integer, result value is a string.
 //
 Func<int, string> func1 = (x) => string.Format("string = {0}", x);
 //
 // Func instance with two parameters and one result.
 // ... Receives bool and int, returns string.
 //
 Func<bool, int, string> func2 = (b, x) =>
     string.Format("string = {0} and {1}", b, x);
 //
 // Func instance that has no parameters and one result value.
 //
 Func<double> func3 = () => Math.PI / 2;

 //
 // Call the Invoke instance method on the anonymous functions.
 //
 Console.WriteLine(func1.Invoke(5));
 Console.WriteLine(func2.Invoke(true, 10));
 Console.WriteLine(func3.Invoke());
    }
}

Output

string = 5
string = True and 10
1.5707963267949


In Main, the program declares and assigns three Func type instances. It uses the full type declaration of the Func types, which includes the parameterized types in the angle brackets.
Note: In the Func types, all of the first parameterized types excluding the last are the argument types.
And: The final parameterized type is the return value. We show two string return values and one double.

The first Func receives an int and returns a string and it is declared as Func<int, string>. The second receives a bool and an int and returns a string. It is declared as Func<bool, int, string>. The third simply returns a value.
Int String Bool
Finally: This program calls the Invoke method on the Func type instances. Each of the Invoke calls uses different parameters.

References:
http://www.dotnetperls.com/func
 

No comments:

Post a Comment