What is the benefit of using “Expression Bodied Fu

2019-02-16 14:17发布

This question already has an answer here:

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!

标签: c# c#-6.0
3条回答
可以哭但决不认输i
2楼-- · 2019-02-16 14:45

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

查看更多
The star\"
3楼-- · 2019-02-16 14:47

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

查看更多
萌系小妹纸
4楼-- · 2019-02-16 15:07

A body expression provide only a compact and cleaner way to declare a readonly property.
From a perfomance 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 deafult 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

查看更多
登录 后发表回答