C#中的event直接调用和Invoke的区别,是否性能有差异

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


版权声明:本文为yinfourever原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。