What is the purpose of the => operator in the use of lists?

Asked

Viewed 241 times

13

What is the purpose of the operator => in the use of lists List<T>, I am starting to use generic lists and came across this operator, it is only used in this method LISTA.ForEach(i => Console.WriteLine(i)); ?

Below follows my example for illustration:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListaGenericaCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> LISTA = new List<string>();

            string opcao = "1";

            while (opcao.Equals("1")) 
            {
                Console.WriteLine("Digite um nome para inserir na lista: ");
                string nome = Console.ReadLine();

                LISTA.Add(nome);

                Console.WriteLine("Deseja inserir outro nome na lista? 1-SIM | 2-NAO:");
                opcao = Console.ReadLine();
            }

            LISTA.Sort();
            Console.WriteLine("A lista tem " + LISTA.Count + " itens:");
            LISTA.ForEach(i => Console.WriteLine(i));

            Console.ReadKey();
        }
    }
}

I also noticed that the variable i no data type has been specified for her, she is a generic type?

2 answers

14


None. This is used to create a lambda, which is an anonymous function. This can be used in any situation where an anonymous function/method is expected. It is not related to the list.

i => Console.WriteLine(i)

i is the parameter that the lambda will receive and the Console.WriteLine(i) is its body that will be executed. In this specific case the method ForEach() will call this lambda for each list item by passing the item to i.

The type of i is inferred by the compiler through the signature of the method ForEach.

Another way to write the same:

(i) => { Console.WriteLine(i); }

Old-fashioned way using delegate:

delegate(string i) { Console.WriteLine(i); };

I put in the Github for future reference.

This is used when we need called functions callback, where you pass a function to be executed, instead of passing the result of the execution.

Question with more information.

Nomenclature.

More information.

Documentation.

In C# 6 this syntax can be used in common methods within a class. But although the syntax is the same, in this case it would not be a lambda. That is, it is not a pointer to a function that will be stored in a variable to be called when you need it. Example:

public void Imprime(object obj) => Console.WriteLine(obj);
  • 1

    Only a small detail. In the last example, the parameter of WriteLine should be obj.

  • 1

    Sure, damn C V :)

  • To complement only, the nickname of the "lambda operator" is Fat Arrow - Fat Arrow in Free Translation.

9

The name of this operation is delegate, and works as a call to a predicate.

The code

LISTA.ForEach(i => Console.WriteLine(i));

Amounts to

foreach (var i in LISTA)
{
    Console.WriteLine(i);
}

Browser other questions tagged

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