Surrounding lambda expression parameter with parentheses
I was following this tutorial today to learn a little about Web API and noticed something - there was this line in the tutorial code:
var product = products.FirstOrDefault((p) => p.Id == id);
As you can see, the "p" parameter is listed in parentheses. Since it is not necessary, so is this, I am curious if there is any benefit to this, or if it is just a developer preference?
source to share
The C # specification explicitly states that it (p) => ...
can be written as p => ...
:
7.15 Anonymous Function Expressions
...
In an anonymous function with one implicitly typed parameter, the parentheses can be omitted from the parameter list. In other words, the anonymous form function
( param ) => expr
can be shortened to
param => expr
So there is no technical difference, just personal preference.
source to share