I was registering to an event in my class, and as per usual I was lazy and just use the autocomplete feature built into Visual Studio 2008 Pro which auto creates the delegate creation and it's associated method.
public abstract class FooBase
{
protected event EventHandler<MyValueChangedArgs> MyValueChanged;
protected FooBase()
{
MyValueChanged +=
new EventHandler<MyValueChangedArgs>(HandleMyValueChanged);
}
private void HandleMyValueChanged(object sender, MyValueChangedArgs e)
{
// Some handling logic
}
}
Usually I dont think twice when Visual Studio gens the event handler for me, but then I received a recommendation from Refactor! Pro to "Remove Redundant Delegate Creation". The recommendation results in:
public abstract class FooBase
{
protected event EventHandler<MyValueChangedArgs> MyValueChanged;
protected FooBase()
{
MyValueChanged += HandleMyValueChanged;
}
private void HandleMyValueChanged(object sender, MyValueChangedArgs e)
{
// Some handling logic
}
}
Under what circumstances is delegate creation redundant and when is delegate creation appropriate?
Thanks.