Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of using "Expression Bodied Functions and Properties" [duplicate]

Tags:

c#

c#-6.0

I do have seen many using that new feature, but what is the benefit of using those expressions?

Examples:

public override string ToString() => string.Format("{0}, {1}", First, Second);

public string Text =>
string.Format("{0}: {1} - {2} ({3})", TimeStamp, Process, Config, User);

This question is different to this one, because I am not only asking if the performance and efficiency does change, but also about the difference of the compiled code (IL code) and if it is best practice and should be used in the real world.

This question is also not off topic, because I am asking for real life enterprise experience and not recommendations. Therefore don't recommend, but explain why you would or why you would not use that feature and what experiences you have with it!

like image 698
TheRealVira Avatar asked Oct 29 '16 16:10

TheRealVira


People also ask

What is an expression bodied method?

An expression-bodied method consists of a single expression that returns a value whose type matches the method's return type, or, for methods that return void , that performs some operation.

Can C# expression bodied method contain multiple expression?

Yes, you can.

Which is correct syntax for expression bodied function?

The Syntax of expression body definition is, member => expression; where expression should be a valid expression and member can be any from above list of type members.


3 Answers

It provides better readability and saves some space in your code. It is just nicer.

like image 52
MacakM Avatar answered Oct 17 '22 18:10

MacakM


Answering one of my questions. I have tested out, if the IL code is the same with the following results:

static void Main(string[] args)
{
    Console.WriteLine(MyClass.GetTexted);
}

class MyClass
{
    private static string dontTouchMe = "test";

    public static string GetTexted { get { return dontTouchMe + "1"; } }

    //public static string GetTexted => dontTouchMe + "1";
}

... will generate the same IL code:

00993376  call        [random value]
0099337B  mov         dword ptr [ebp-40h],eax  
0099337E  mov         ecx,dword ptr [ebp-40h]  
00993381  call        71886640  
00993386  nop  
        }
00993387  nop  
00993388  lea         esp,[ebp-0Ch]  
0099338B  pop         ebx  
0099338C  pop         esi  
0099338D  pop         edi  
0099338E  pop         ebp  
0099338F  ret  

The [random value] part is the only code that has changed, which does make sense since it changes every time it is getting compiled.


I am still a student at school, so I can only answer the other questions from my perspective (which is not an enterprise view):

  • ”Expression Bodied Functions and Properties” do seem like a good practice due to a more compact and readable code. (thx to @Wazner and @MacakM)

  • It does also make sense to use them, because they look more modular and more like OOP code

like image 9
TheRealVira Avatar answered Oct 17 '22 20:10

TheRealVira


A body expression provides only a compact and cleaner way to declare a readonly property.
From a performance point of view, you write this:

public bool MyProperty {
    get {
        return myMethod();
    }
}

private bool myMethod() {return true;}

or this:

public bool MyProperty => myMethod();
private bool myMethod() {return true;}

There is no difference because it's translated into IL in the same way.
But pay attention to this thing: the body expression is a feature available by default on Visual Studio 2015 and newer version but if you want use in older VS version you need to install C# 6.0 from NuGet.

like image 7
Tinwor Avatar answered Oct 17 '22 20:10

Tinwor