Difference between Delegate.Invoke and Delegate()

delegate void DelegateTest();

DelegateTest delTest;

Whats the difference between calling delTest.Invoke() and delTest()? Both would execute the delegate on the current thread, right?


Solution 1:

The delTest() form is a compiler helper, underneath it is really a call to Invoke().

Solution 2:

Richard's answer is correct, however starting with C# 6.0, there is one situation where using Invoke() directly could be advantageous due to the addition of the null conditional operator. Per the MS docs:

Another use for the null-conditional member access is invoking delegates in a thread-safe way with much less code. The old way requires code like the following:

var handler = this.PropertyChanged;
if (handler != null)  
    handler(…);

The new way is much simpler:

PropertyChanged?.Invoke(…)   

The new way is thread-safe because the compiler generates code to evaluate PropertyChanged one time only, keeping the result in a temporary variable. You need to explicitly call the Invoke method because there is no null-conditional delegate invocation syntax PropertyChanged?(e).

Solution 3:

That's correct. Both have the exact same result.

Given that you have properly initialized delTest of course.