class Student:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get { return name; }
set
{
name = value;
//激发事件
if ( this.PropertyChanged != null )
{
//this.PropertyChanged( this , new PropertyChangedEventArgs( "Name" ) );//这样跟下面有什么不同?
this.PropertyChanged.Invoke( this , new PropertyChangedEventArgs( "Name" ) );
}
}
}
}
两条语句都可以实现一样的功能,只是写法不同而已吗?Which is faster; using event.Invoke(args)
, or just calling event(args)
. What's the difference? Is one faster or slower than the other; or is it just a matter of preference?(哪种性能会更好?)
答:完全一样,编译期会将 event(args)转成event.Invoke(args)。两者都转成相同的IL代码。对于DynamicInvoke,会使用反射,从而性能较差
StackFlow上相关问题:https://stackoverflow.com/questions/5928077/event-invokeargs-vs-eventargs-which-is-faster/47198870