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?

+3


source to share


2 answers


In this case, it is purely a developer preference.

Patients are needed if you have multiple parameters. For example:



var singleString = someStrings.Aggregate((current, next) => current + Environment.NewLine + next);

      

+8


source


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.

+3


source







All Articles