Is expression-bodied recommended? Is there a performance difference?

Asked

Viewed 64 times

5

The extent to which it is recommended, or even good practice to use Expression-bodied?

I know that the Expression-bodied allow properties, methods, operators and other function members to have defined bodies using expressions lambda (=>) rather than instruction blocks, which helps to reduce the amount of code and gives a clearer view on expressions.

This practice impacts performance or is only "visual", leaving the code more streamlined?

Example below:

Without Expression-bodied:

public class Item
{
    public int Quantidade { get; set; }
    public double Preco { get; set; }
    public double Total
    {
        get
        {
            return Quantidade * Preco;
        }
    }
}

With Expression-bodied:

public class Item
{
    public int Quantidade { get; set; }
    public double Preco { get; set; }
    public double Total => Quantidade * Preco;
}

2 answers

4


It was created, so it’s recommended. In general there are no contraindications in the use that it was designed, which is something that just has a simple expression and already returns it.

I would like people to understand that language puts these things because it wants to be less verbose and wants to reduce the number of lines. And so people thought that other things they do that increase the number of lines and it’s not as appropriate as they think, because they learned that that was "good practice" (which I always criticize here on the site and lecturing about this).

It’s not the same as a var for example, that there are cases that there are difficulties in its use. This only does not use who does not want, and taste is not discussed, only regrets :)

Performance changes nothing at all. Semantics also not, or any other criterion, is only more readable in the second form, unless the person does not know how to program. But the solution to this is for one to learn.

See on Shaplab how does the shape Expression bodied and the verbose form.

Just don’t think this is about expression lambda, is not, the semantics of this and lambda is very different and has performance difference. Only the syntax is the same, but it is another mechanism.

  • Thanks for the explanation! I was using quite rightly for helping to decrease the number of lines and also make the code simpler, in my opinion. I just wanted to clear those doubts right there, thank you!!

3

How the goal is to replace the classic structures ({ .. } for example) and make the code more readable and concise, there is no reason not to use.
Your specific example will compile in the same way in Intermediate Language, which will generate the same result at runtime, which will have no difference in performance.

Of course, more complex bodies of methods or even property, with several lines of code and nested structures, such as if and while for example, they may no longer be readable using Expression bodied, hence the good old syntax should fall better.

In this link about what’s new in C#6.0, says: msdn.microsoft.com/

Expression bodied functions are Another syntax simplification in C# 6.0

That is, are another syntax simplification in C # 6.0, which comes to simplify and not harm.

Browser other questions tagged

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