Code Inspection: Redundant lambda parameter explicit type specification
When you instantiate a delegate by using a lambda expression, and pass input parameters to the lambda expression, it might be unnecessary to specify types of those parameters because the compiler can infer them in most cases. For example, if the types accepted by the method were already specified in the delegate declaration, you can omit the types when you specify the method's input parameters.
In the example below, the declared delegate sum
is compatible with methods that return an integer value and accept two parameters of the integer type. Therefore, it is safe to remove type specifiers for the parameters x
and y
, and JetBrains Rider suggests that you do so:
public static int LambdaTest()
{
Func<int, int, int> sum = (int x, int y) => x + y;
return sum(10, 20);
}
public static int LambdaTest()
{
Func<int, int, int> sum = (x, y) => x + y;
return sum(10, 20);
}
Last modified: 07 April 2022