动作(ARG)和Action.Invoke之差(ARG) [英] Difference between Action(arg) and Action.Invoke(arg)

查看:231
本文介绍了动作(ARG)和Action.Invoke之差(ARG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

现在我的问题(s)是


  • 两者有什么方法来调用Action之间的区别,如果有的话?

  • What is the difference between the two ways to call the Action, if any?

是一个比其他更好吗?

Is one better than the other?

在使用它?

谢谢

推荐答案

所有委托类型有一个编译器生成的调用方法。< BR>
C#,您可以委托自己作为一个快捷键调用调用此方法

All delegate types have a compiler-generated Invoke method.
C# allows you to call the delegate itself as a shortcut to calling this method.

他们都编译到相同的IL:

They both compile to the same IL:

Action<string> x = Console.WriteLine;
x("1");
x.Invoke("2");



IL:



IL:

IL_0000:  ldnull      
IL_0001:  ldftn       System.Console.WriteLine
IL_0007:  newobj      System.Action<System.String>..ctor
IL_000C:  stloc.0     
IL_000D:  ldloc.0     
IL_000E:  ldstr       "1"
IL_0013:  callvirt    System.Action<System.String>.Invoke
IL_0018:  ldloc.0     
IL_0019:  ldstr       "2"
IL_001E:  callvirt    System.Action<System.String>.Invoke

(本 ldnull 目标参数的的open代表

这篇关于动作(ARG)和Action.Invoke之差(ARG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆