What benefits of using the = operator in common methods that are not related to Lists or Lambda Expression?

7

I asked the following question: # and the user @bigown answered me, however, in the answer he used an operator and this gave me a new question. I questioned the same about the use of the operator and he indicated to me the following link for clarification: What is the purpose of the operator = > not using lists? . However, it is still unclear to me.

What are the benefits of using the => operator in methods that are not related to lists or lambda expressions?

If the operator has no relation to lists or lambda expression. How would the operation be called if it is not or is not related to an anonymous function?

I tried to run the following code in ConsoleApplication, however, from the operator declaration error = > saying that the ";" is expected. If this operator has this function should not present such error.

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

namespace ConsoleApplication1
{
    public class C
    {
        public int Main() {
            var conta = new Conta();
            var txtValor = new Form();
            bool retorno;
            if ((retorno = conta.Saca(Convert.ToDouble(txtValor.Text)))) {
                Console.WriteLine(retorno);
                return 1;
            }
            return 0;
         }
    }

    public class Conta {
        public bool Saca(double x) => true;
    }

    public class Form {
        public String Text;
    }
}
    
asked by anonymous 23.05.2017 / 23:41

1 answer

8

In this case it is not an operator, it is just a construction of the language to simplify the writing of methods that contain only one line.

This was introduced in C # 6 for normal methods and in C # 7 it was made available to other methods like properties, constructors, destructors, and events. It does not work in previous versions.

It does not have anything special in the language, it does not do anything new, it does not change any semantics, it's just a syntactic change.

public bool Saca(double x) => true;

is exactly the same as:

public bool Saca(double x) {
    return true;
}

It has% implicit%. But it works with a method that returns return as well.

Obviously you can only have one line.

using static System.Console;
using System;

public class Program {
    public static void Main() {
        var objeto = new Exemplo(42);
        objeto.Imprime();
        WriteLine(objeto.Executa());
        WriteLine(objeto.Executa2());
        objeto.Prop = 42;
        WriteLine(objeto.Prop);
        objeto.Prop = 12;
    }
}

public class Exemplo {
    private int prop;
    public int Prop { get => prop; set => prop = value == 42 ? value : throw new ArgumentException("Tem que ser 42 ", nameof(Prop)); }

    public Exemplo(int x) => prop = x;

    public void Imprime() => WriteLine("ok");
    public string Executa() => "texto";
    //public bool Teste2() => WriteLine("ok2"); return true; //daria erro
    public string Executa2() { //é a mesma coisa
        return "texto";
    }
}

See running on .NET Fiddle . And on the Coding Ground (it's still C # 6 so everything does not work either. I've placed it on GitHub for future reference .

    
23.05.2017 / 23:48