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:
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: 08 April 2024