C# Action(arg) 和 Action.Invoke(arg) 的区别
they both compile to the same IL. One is shorthand for the other. The only possible benefit I can see is that you can check for null and call it using newer c# features when using invoke, e.g. x?.Invoke(“1”) will call the delegate only if it’s not null.
All delegate types have a compiler-generated Invoke method.
C# allows you to call the delegate itself as a shortcut to calling this method.
在编译成 IL 的时候,都是一样的,所以没有区别,为了防止空的调用导致异常,我们建议一般使用
1 | Action?.invoke(arg) |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 一只大菜狗!