Func<T>() vs Func<T>.Invoke()
Asked Answered
C

2

123

I'm curious about the differences between calling a Func<T> 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?

Cliff answered 30/4, 2013 at 21:30 Comment(1)
L
161

There's no difference at all. The second is just a shorthand for Invoke, provided by the compiler. They compile to the same IL.

Laughingstock answered 30/4, 2013 at 21:34 Comment(9)
Leaving the Invoke() method off is resulting in the compiler error: "Cannot implicitly convert type 'System.Func<T>' to 'T' ". I'm compiling against .NET 4.Dolf
@Mike: That would happen if you'd missed the brackets off as well - i.e. tried to return (T)method rather than (T)method().Laughingstock
@JonSkeet So is this guy wrong here: social.msdn.microsoft.com/Forums/en-US/…Murphey
@superlogical: There are two problems here. Firstly, the question there is about the difference between calling a method directly and calling it via a delegate. That's not the same as the difference between foo() and foo.Invoke() where foo is a variable of a delegate type. The other problem is that the answer seems to be talking about Control.Invoke, which isn't the same as calling Invoke on a delegate.Laughingstock
Has the OP edited his original post after reading Jon Skeet's comment? I can't track down what jon is referring to.Zecchino
@xDisruptor: There are two pieces of code, and the OP is asking whether they're equivalent. My response is that they compile to the same IL. I'm not sure what's confusing you.Laughingstock
Upon reading your comment again I realized you were commenting on the link provided by superlogical - not the original post. For someone that is reading quickly through the comments it's not so clear what it is you are commenting on: "The answer seems to be talking about Control.Invoke etc)." <- I thought you where referring to the very answer you provided here, but you are referring to the other answer provided in social.msdnZecchino
Which syntax do you prefer and why? I personally think that using Invoke() does not add anything in terms of readability or maintainability so I'd rather use the syntax without it.Kirwan
@LucaCremonesi: If it's the result of a method call, I generally like Invoke, as GetAction()() looks weird, but GetAction().Invoke() looks okay. But I don't mind either way, for the most part.Laughingstock
D
30

Invoke works well with new C# 6 null propagation operator, now you can do

T result = method?.Invoke();

instead of

T result = method != null ? method() : null;
Disassembly answered 25/2, 2018 at 10:51 Comment(3)
Can you illustrate a scenario where this is useful? The textbook case is raising events.Estimate
For example you can have an optional Func parameter that if no value is assigned, is the default(Func) which is null and is ignored.Rockery
It's the common case for events and other multi-cast delegates - they are usually initialized with null and gain non-null value after calling +=. So if you are going to fire an event and don't know if there are any subscriptions, the concise solution is to call ?.Invoke(...).Look

© 2022 - 2024 — McMap. All rights reserved.