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!
Answering one of my questions. I have tested out, if the IL code is the same with the following results:
... will generate the same IL code:
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
It provides better readability and saves some space in your code. It is just nicer.
A body expression provide only a compact and cleaner way to declare a readonly property.
From a perfomance point of view you write this:
or this:
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