ReSharper 2017.3 Help

Code Inspection: Explicit delegate creation expression is redundant

Redundant delegate constructor calls may occur, for example, as a result of adding event handlers:

MyHandler += new EventHandler(MyMethod);

Below, the type of a delegate variable ValueChanged can be inferred from its declaration. To make code shorter, ReSharper suggests removing a redundant explicit delegate’s constructor call:

Suboptimal codeAfter the quick-fix
internal class EventTest { protected event EventHandler ValueChanged; protected EventTest() { ValueChanged += new EventHandler(HandleValueChanged); } private void HandleValueChanged(object sender, EventArgs e) { // ... } protected virtual void OnMyValueChanged() { ValueChanged?.Invoke(this, EventArgs.Empty); } }
internal class EventTest { protected event EventHandler ValueChanged; protected EventTest() { ValueChanged += HandleValueChanged; } private void HandleValueChanged(object sender, EventArgs e) { // ... } protected virtual void OnMyValueChanged() { ValueChanged?.Invoke(this, EventArgs.Empty); } }
Last modified: 7 December 2017

See Also