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

Asked

Viewed 98 times

9

I asked the following question: What value is checked in a condition operation with a variable value assignment? and the user @Maniero answered me, however, in the reply he used an operator and that generated me a new doubt. I asked the same about the use of the operator and he indicated me the following link for clarification: What is the purpose of the operator => in the use of lists?. However, it is not yet clear to me.

What are the benefits of using the operator => in methods unrelated to lists or lambda expression?

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

I tried to execute the following code in Consoleapplication, however, from the operator declaration error =>, saying that ";" is expected. If that operator has that function it should not present such an 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;
    }
}
  • 1

    It’s just to shorten the code.

  • All right, but it doesn’t work the way @bigown explained, or I got it / tested wrong

  • Or you’re not using C# 6 (?)

  • No, I am using the 2012 in case, I will test with the 2017

  • This only came to exist in C# 6.

  • I get it, I didn’t know that. I’m gonna test

Show 1 more comment

1 answer

10


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

This was introduced in C# 6 for normal methods and in C# 7 was made available for other methods such as properties, constructors, destructors and events. Does not work in previous versions.

It has nothing special in the language, does nothing new, does not change any semantics, is just a syntactic change.

public bool Saca(double x) => true;

is exactly the same as:

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

He has a return implicit. But it works with a return method void also.

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";
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Now it became clear, greatly simplifies the time of writing methods with a single line of code. I learned something new. Thanks for the explanation.

Browser other questions tagged

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