You want to use the Func generic type in your program using the C# programming language. The Func type is a parameterized type and you must specify the number and kinds of parameters, and also the type of the return value. The Func type provides a way to store anonymous methods in a generalized and simple way. Here we explore the Func type essentials in the C# programming language, seeing it used with lambda expressions and also with the invocations of the methods.
First, this program text written in the C# programming language 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 >. The first type parameters are the arguments to the methods, and the final type parameter is the return value.
~~~ 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 of the program ~~~
string = 5
string = True and 10
1.5707963267949Using Func with one parameter and one return value. This program text use the Main entry point and 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. In the Func types, all of the first parameterized types excluding the last are the argument types. The final parameterized type is the return value.
Using Func with other parameters and one return value. The program text uses three Func instances. The first one receives an int and returns a string and it is declared as Func<int, string>. The second one receives a bool and an int and returns a string; it is declared as Func<bool, int, string>. The third one is parameterless and returns one value.
Invoke method on Func type. Finally, this program text shows how you can call the Invoke method on the Func type instances. Note that each of the Invoke calls uses different parameters. You cannot call the Invoke method with an invalid number of parameters because the Func type itself stores the required parameter types and uses those to generate the Invoke method signature.
In this section, we note how you can use the Func type as a field, return value, local variable, or parameter in your C# programs. You must specify the full type name with the type parameters. For example, you can add the Func<int, string> as a field, and give it an identifier. This is a way of storing delegates by their signatures.
Here we looked at the Func parameterized type in the C# programming language and used it with the lambda expression syntax. We stored anonymous functions inside Func pointers and then invoked those methods through the Invoke method on the Func instances. The Func type is specialized by the number and type of parameters, and the type of return value. Other similar types include the Predicate type, and the Action type, and they can be used with similar syntaxes. You can find more details on the lambda expression syntax in the C# language.