event.Invoke(args) vs event(args). Which is faster?

前端 未结 4 1325
南旧
南旧 2020-12-13 18:31

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 ju

4条回答
  •  被撕碎了的回忆
    2020-12-13 19:27

    Since the introduction of null-conditionals in C# 6.0, Invoke can be used to simplify thread-safe null-checking of delegates. Where you would previously have to do something like

    var handler = event;
    if (handler != null)
        handler(args);
    

    the combination of ?. and Invoke allows you to simply write

    event?.Invoke(args)
    

提交回复
热议问题