static void Main()
{
Action<string> myAction = SomeMethod;
myAction("Hello World");
myAction.Invoke("Hello World");
}
static void SomeMethod(string someString)
{
Console.WriteLine(someString);
}
The output for the above is:
Hello World
Hello World
Now my question(s) is
-
What is the difference between the two ways to call the Action, if any?
-
Is one better than the other?
-
When use which?
Thanks
All delegate types have a compiler-generated
Invokemethod.C# allows you to call the delegate itself as a shortcut to calling this method.
They both compile to the same IL:
C#:
IL:
(The
ldnullis for thetargetparameter in an open delegate)