任何人都可以回答为什么在Microsoft Documentation for Collection(T)中定义了以下行
EventHandler<DinosaursChangedEventArgs> temp = Changed;
与直接引用Changed相比,它有什么优势?谢谢!
public class Dinosaurs : Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string newItem)
{
base.InsertItem(index, newItem);
EventHandler<DinosaursChangedEventArgs> temp = Changed;
if (temp != null)
{
temp(this, new DinosaursChangedEventArgs(
ChangeType.Added, newItem, null));
}
}
我似乎无法弄清楚为什么不使用以下方法?
public class Dinosaurs : Collection<string>
{
public event EventHandler<DinosaursChangedEventArgs> Changed;
protected override void InsertItem(int index, string newItem)
{
base.InsertItem(index, newItem);
if (Changed != null)
{
Changed(this, new DinosaursChangedEventArgs(
ChangeType.Added, newItem, null));
}
}