I'm working in C# and I have a little doubt.
Is there a difference between calling a delegate directly or using the method Invoke
?
For example:
Func<string> @Func = () => "Test";
var result = @Func.Invoke();
var result2 = @Func();
Console.WriteLine("{0},{1}",result,result2);
// El resultado es Test,Test
Is there a difference between the line @Func.Invoke();
and @Func();
?
If there is such a difference, in what cases should each be used?
In these cases, it is best to see what the compiler does when it converts the code to IL:
As you can see, the IL of
@Func.Invoke()
is exactly the same as that of@Func()
:So both are exactly identical.
I would recommend you see
Func() vs Func.Invoke()
There is a reference to an interesting answer from which I draw
Call the direct method
Translation
Using Invoke
Translation