Func< T> .Invoke() [英] Func<T>() vs Func<T>.Invoke()
本文介绍了Func< T> .Invoke()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很好奇直接调用Func与在其上使用Invoke()的区别。有区别吗?是第一个语法糖,还是在下面调用Invoke()吗?
public T DoWork < T>(Func< T>方法)
{
return(T)method.Invoke();
}
vs
<学前班= lang-cs prettyprint-override>
public T DoWork< T>(Func< T>方法)
{
return(T)method();
}
还是我完全走错了路:)谢谢。
解决方案
完全没有区别。第二个只是编译器提供的 Invoke
的简写。它们编译为相同的IL。
I'm curious about the differences between calling a Func directly vs using Invoke() on it. Is there a difference ? Is the first, syntactical sugar, and calls Invoke() underneath anyway ?
public T DoWork<T>(Func<T> method)
{
return (T)method.Invoke();
}
vs
public T DoWork<T>(Func<T> method)
{
return (T)method();
}
Or am I on the wrong track entirely :) Thanks.
解决方案
There's no difference at all. The second is just a shorthand for Invoke
, provided by the compiler. They compile to the same IL.
这篇关于Func< T> .Invoke()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文