Sometimes multiple copies of identical lamda expressions are placed in various parts of the code. It is now possible to convert a lambda expression into a named method, but it would be convenient to have a "replace all" option to identify identical lambda expressions and replace them all with a common named method. In case where lambda expressions are all within the same method, an alternative would be to introduce a local delegate variable and use that.
This will work easily if no variables are used from outside of the lambda expression (non-lambda parameters).
void Bar()
{
Foo( s => s == 2 ? s + 10 : s + 5 );
Foo( r => r == 2 ? r + 10 : r + 5 );
}
would get replaced into:
void Bar2(int s)
{
return s == 2 ? s + 10 : s + 5;
}
void Bar()
{
Foo(Bar2);
Foo(Bar2);
}
or as a variable:
void Bar()
{
var func = s => s == 2 ? s + 10 : s + 5;
Foo(func);
Foo(func);
}
Description
Sometimes multiple copies of identical lamda expressions are placed in various parts of the code. It is now possible to convert a lambda expression into a named method, but it would be convenient to have a "replace all" option to identify identical lambda expressions and replace them all with a common named method. In case where lambda expressions are all within the same method, an alternative would be to introduce a local delegate variable and use that.
This will work easily if no variables are used from outside of the lambda expression (non-lambda parameters).
void Bar()
{
Foo( s => s == 2 ? s + 10 : s + 5 );
Foo( r => r == 2 ? r + 10 : r + 5 );
}
would get replaced into:
void Bar2(int s)
{
return s == 2 ? s + 10 : s + 5;
}
void Bar()
{
Foo(Bar2);
Foo(Bar2);
}
or as a variable:
void Bar()
{
var func = s => s == 2 ? s + 10 : s + 5;
Foo(func);
Foo(func);
}