2

Is there a difference performance or memory wise between

public string Id
{
    get { return this.id; }
}

or

public string Id => this.id;

I prefer the look of => this.Id but I was wondering if I am doing something wrong?

Also, is there any issue with something like

public void Test() => this.id = "fdsafdsa";
  • I know in document says : For expression bodied members = Only assignment, call, increment, decrement, and new object expression can be used as a statement. In other case these are same and expression body it is better to use for clear code. – wikiCan Nov 07 '19 at 13:54
  • The same goes for `public string Id { get => this.od; }`. It all compiles down to the same code. – John Alexiou Nov 07 '19 at 14:06
  • 1
    Possible duplicate of [What is the difference between Expression bodied syntax vs Getter syntax on IL level?](https://stackoverflow.com/questions/46522877/what-is-the-difference-between-expression-bodied-syntax-vs-getter-syntax-on-il-l) –  Nov 07 '19 at 14:15

2 Answers2

1

No, there's no difference.*

They compile to the same code. See it here.

It's just syntactic sugar for properties' (or other members') implementation. Or as the docs defines it:

Expression body definitions let you provide a member's implementation in a very concise, readable form. You can use an expression body definition whenever the logic for any supported member, such as a method or property, consists of a single expression.


* When it's possible to use either form.

  • Sorry but not same thing, compile same IL but there was some limitations for expression body. – wikiCan Nov 07 '19 at 14:00
  • 2
    @wikiCan Of course there are limitations (that are explained in the docs). The OP, however, is asking whether there's a difference in terms of performance (i.e., when it's possible to use either form). Those limitations won't let you use expression body definitions in the first place so they're not subject to this question. – 41686d6564 stands w. Palestine Nov 07 '19 at 14:02
  • @AhmedAbdelhameed Not the same IL code in Debug build: lambda expression-bodied is more optimized with the sample. Same is in Release. –  Nov 07 '19 at 14:11
  • 1
    @OlivierRogier From your proposed dup: _"So, the added instructions don't do anything. I believe most of them are there to aid in debugging"_. So, in release mode, they compile to the same IL and in debug mode, there are added instructions that do nothing and are effectively redundant. What's your point? – 41686d6564 stands w. Palestine Nov 07 '19 at 14:33
  • @AhmedAbdelhameed I'm not involved in IL nor debugger, only in i486. [Br_S](https://docs.microsoft.com/dotnet/api/system.reflection.emit.opcodes.br_s) & [Ldloc_0](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldloc_0). All I know is that it consumes CPU Ticks. There no *conceptual* difference, nor speed difference in release build. In debug, it slows down the process. So after investigating, I see that in case of a method having a `}` at the end, this additional code is to allow stepping on it and spying like you can verify. With EB there is no needed step nor spy. –  Nov 07 '19 at 14:53
0

I think it's more like a syntax sugar.

In this question it's explained better What is the => assignment in C# in a property signature answer from Alex Booter.

luturol
  • 565
  • 8
  • 12