21

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!

Community
  • 1
  • 1
TheRealVira
  • 1,444
  • 4
  • 16
  • 28
  • 1
    Brevity. Writing the same thing, but shorter. It allows the developer to omit the braces and the return statement. – Wazner Oct 29 '16 at 16:58
  • 1
    Readabilty? Compactness? What are the benefits of auto-properties? – Ivan Stoev Oct 29 '16 at 16:58
  • Interesting approche. Does it also provide performance enhancements? – TheRealVira Oct 29 '16 at 17:05
  • 1
    http://stackoverflow.com/questions/28411335/expression-bodied-function-members-efficiency-and-performance-in-c-sharp-6-0 – Ofiris Oct 29 '16 at 17:06
  • @Ofiris I do have read that question, but does somebody really know, if it is exactly the same? Like is the compiled code the same length and the same content? Or are there some changes? Some real world examples? – TheRealVira Oct 29 '16 at 17:10
  • @TheRealVira Some of the answeres there shows that the compiled IL code is the same for the same content. You can test it yourself using LinqPad. – Ofiris Oct 29 '16 at 17:15
  • @Ofiris I would like to see some examples and how the interpreter would handle such expressions. – TheRealVira Oct 29 '16 at 17:20
  • @TheRealVira It's basically up to the compiler implementation but as I said, go ahead and view some IL generated code. – Ofiris Oct 29 '16 at 17:24
  • [Just test it and view by yourself](http://tryroslyn.azurewebsites.net/#f:>ilr/MYGwhgzhAEBiD28BQBvJ0PQA4FcBGIAlsNBAC4BOhAdgObQAqApgB5kCM0AvAHzQBEAM0T8A3Oky4CxUpRr1mbAEzQU0WkzKroFTTgrUBw+GOgBfc0jNA===) – Sehnsucht Oct 29 '16 at 18:06
  • @ All -> I do want to have an enterprise like view on that question, if it is good or bad practice and if it is used in enterprise code. I do have answered the question from my [point of view](http://stackoverflow.com/questions/40321431/what-is-the-benefit-of-using-expression-bodied-functions-and-properties/40321985#40321985). – TheRealVira Oct 29 '16 at 18:10
  • 1
    For the differences, regarding performance and generated IL, see the linked question: It’s the same. As for whether it’s good practice, or what the benefit of using it is; it is a subjective opinion (thus off topic for SO). Use it, if you like it, don’t use it, if you don’t like it. It’s shorter, so that’s already enough for many people. But I would personally avoid using it, where the involved expressions gets longer or more complicated. – poke Oct 29 '16 at 18:21
  • @Ofiris *“It's basically up to the compiler implementation”* – It’s not up to the implementation. The equivalence of expression bodied functions and properties to their normal code is guaranteed by the language specification. Compilers are required to produce the same IL for them. – poke Oct 29 '16 at 18:28
  • @poke, thanks for the comment, please note that MS compiler will generate different IL code when not specifing `/optimize` – Ofiris Oct 29 '16 at 18:56
  • IMHO: (1) This is a pure marketing feature! at the end of the day, C# and Visual Studio is a product, so somewhere in the management level someone decides which feature to include in the language, from the perspective of that person this might be a useful feature, from the perspective of some developers it might not. (2) Programming is not always an objective science like physics or math, the rules of physics and math apply everywhere, so no two mathematicians argue like this: – Siraf Nov 05 '18 at 12:55

3 Answers3

10

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

Wazner
  • 2,962
  • 1
  • 18
  • 24
MacakM
  • 1,804
  • 3
  • 23
  • 46
  • This is an short and good answer about what is good on using those expressions. Plus 1 for that! – TheRealVira Oct 29 '16 at 18:19
  • 8
    Even though it makes methods more concise, I've found the extended method declarations to be easier to follow by eye since they preserve the indenting consistently across the class members. – orad Mar 05 '19 at 09:53
  • 1
    Brevity is a nice thing, but more important to me is the fact that the `=>` indicates a functional Result that very likely has no side-effects. This makes me much more confident in Code-Reviews, provided the functions used (recursively) don't have side effects either. – Spoc Jun 19 '22 at 16:56
9

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

Community
  • 1
  • 1
TheRealVira
  • 1,444
  • 4
  • 16
  • 28
8

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tinwor
  • 7,765
  • 6
  • 35
  • 56
  • Other Visual Studio IDEs do also support c# 6.0: http://stackoverflow.com/questions/28638547/c-sharp-6-0-support-in-visual-studio-2012 – TheRealVira Oct 29 '16 at 18:14
  • 3
    You can use Expression-body in method declaration too `public bool MyProperty => myMethod(); private bool myMethod() => true;` – Nishanth Vemula Jan 09 '18 at 11:21