What is the use of Func<T, Tresult>

Asked

Viewed 1,704 times

8

I was researching some things and came across the Func<T, TResult>, and I didn’t quite understand his usefulness.

For example:

//Método
public string Nome(string nome)
{
    return "Meu nome é " + nome
}

//Ao utiliza-lo eu chamaria assim
string nome = Nome("Josias");

And using the Func<T, TResult>:

Func<string, string> metodo = Nome;

string nome = metodo("Josias");

I couldn’t see any difference in using the Func<T, TResult>.

Where it would be useful to use the Func<T, TResult>?

3 answers

9


What use

Passing to a method (or a function) another function. It’s a kind of delegate.

How to read Func<T, TResult>

Function with a type argument T that returns a type value Tresult.

I couldn’t see any difference in using the Func<T, TResult>.

In fact you did not pass any function to a variable used in another function. You just called the variable with the argument. So usefulness is not well understood.

Where it would be useful to use the Func<T, TResult>?

The dcastro response has a good example. I’ll put another one in his:

IEnumerable<int> AplicarOperacaoMatematicaEmLista(IEnumerable<int> lista, Func<int, int> funcao)
{
    foreach (var item in lista)
        yield return funcao(item);
}

Use:

var potenciaDeDois = delegate(int numero) { return numero * numero; };
var listaDeInteiros = new List<int> { 2, 4, 6, 8, 10 };
var listaDeQuadrados = AplicarOperacaoMatematicaEmLista(listaDeInteiros, potenciaDeDois);
// Vai imprimir uma lista com { 4, 16, 36, 64, 100 }

8

Imagine that you are writing a method that transforms each element of a list into another element. You want this transformation to be arbitrary, user-defined. This would be implemented like this:

IEnumerable<TResult> Map<T, TResult>(IEnumerable<T> collection, Func<T, TResult> transform)
{
    foreach(var item in collection)
        yield return transform(item);
}

Thus, the user can pass as argument any function that receives one of the elements of the list (type T) as argument and to transform it into another element (of type TResult).

var list = new List<int> {1,2,3};
var doubled = Map(list, item => item * 2);

Moral of the story: Func<T, TResult> (and any other delegate in general) serves to pass arbitrary functions. Serves to treat functions as objects/values (this is the pillar of the FP - Functional Programming paradigm).

This method I used as an example (Map) is actually identical to the extension Enumerable.Select. In fact, LINQ extensions rely heavily on passing delegates as arguments (Where, Any, All, GroupBy, OrderBy, SelectMany, SkipWhile, etc.).

  • It is good to make clear also that Func<> as well as others delegates generics made available on . NET serve as a convenience to facilitate the life of the programmer. They wouldn’t need to exist, but they’re generic programming shortcuts so you don’t have to declare a delegate new every time one is needed. That is, it is part of the utility of it to simplify the declaration of a type of delegates.

1

  • The question is not "what is the Func<T,TResult>?", but "What is its usefulness?"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.