Is there a difference between x => x + 1 and x => x += 1?

Val*_*eri 1 c# lambda

I'm currently learning functional programming, and I was wondering if there is a difference between these functions.

Func<int, int> incrementByOne = x => x += 1;
Func<int, int> incrementByOne2 = x => x + 1;
Run Code Online (Sandbox Code Playgroud)

Ren*_*nat 8

There is no difference. Because:

  1. int is a value type, so passed by value (being copied), thus in x => x += 1 changing x cannot affect the source (no side effects)
  2. += returns the result of addition, so the same as + does

  • “语法上”不是主要标准。通常可以用不同的方式写东西。 (3认同)