Skip to main content

Delegate can be replaced with lambda expression

In This Article

CodeRush Classic shows the Delegate can be replaced with lambda expression code issue if a delegate is used in an anonymous method declaration.

#Fix

Use a lambda expression instead of the delegate.

#Purpose

The use of lambda expressions improves your code readability, because of its compact notation.

#Example

Func<Array, int> getLastIndex = delegate(Array arr){ return arr.Length - 1; };

Fix:

Func<Array, int> getLastIndex = arr => arr.Length - 1;