I was just browsing and came across this question:
Action vs delegate event
The answer from nobug included this code:
protected virtual void OnLeave(EmployeeEventArgs e) {
var handler = Leave;
if (handler != null)
handler(this, e);
}
Resharper also generates similar code when using the "create raising method" quick-fix.
My question is, why is this line necessary?:
var handler = Leave;
Why is it better than writing this?:
protected virtual void OnLeave(EmployeeEventArgs e) {
if (Leave != null)
Leave(this, e);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…