Long and short, this second part of the code is correct and safe (even if there are no handlers).
Consider an example application:
namespace ConsoleApplication61
{
class Program
{
static void Main(string[] args)
{
var f = new Foo();
f.MyEvent += new EventHandler(Handler);
f.Trigger();
f.MyEvent -= new EventHandler(Handler);
f.Trigger();
Console.Read();
}
static void Handler(object sender, EventArgs e)
{
Console.WriteLine("handled");
}
}
class Foo
{
public event EventHandler MyEvent;
public void Trigger()
{
if (MyEvent != null)
MyEvent(null, null);
}
}
}
This pattern prints "processed" once.
So, in your example, they are functionally the same, and both will work as needed. Removing a handler that has not been added is also a safe action, it simply does not find anything to delete and does nothing.
As stated in the comments, Marc's answer in more detail:
, - . , , :
Func<object, EventArgs> meth = (s, e) => DoSomething();
myEvent += meth;
myEvent -= meth;
, , , : -)
lambda
:
public class SMSManager : ManagerBase
{
public SMSManager(DataBlock smsDataBlock, DataBlock telephonesDataBlock)
: base(smsDataBlock)
{
SheetEvents.ButtonClick += OnButtonClick;
}
public override void Dispose()
{
SheetEvents.ButtonClick -= OnButtonClick;
base.Dispose();
}
}