加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用参数传递方法的快速和脏的方式作为参数?

发布时间:2020-12-15 23:54:24 所属栏目:百科 来源:网络整理
导读:让我先说明我对C#非常环保的事实.话虽这么说,我正在寻找一种方法来传递一个参数作为参数的方法.理想情况下,我想要做的是: static void Main(string[] args){ methodQueue ( methodOne( x,y ));}static void methodOne (var x,var y){ //...do stuff}static
让我先说明我对C#非常环保的事实.话虽这么说,我正在寻找一种方法来传递一个参数作为参数的方法.理想情况下,我想要做的是:

static void Main(string[] args)
{
    methodQueue ( methodOne( x,y ));
}

static void methodOne (var x,var y)
{
    //...do stuff
}

static void methodQueue (method parameter)
{
    //...wait
    //...execute the parameter statement
}

谁能指出我正确的方向?

解决方法

这应该做你想要的.它实际上是在函数中传递一个无参数的方法,但delegate(){methodOne(1,2);}正在创建一个匿名函数,该函数使用适当的参数调用methodOne.

我想在键入它之前测试它,但只有.net framework 2.0因此我的方法.

public delegate void QueuedMethod();

static void Main(string[] args)
{
    methodQueue(delegate(){methodOne( 1,2 );});
    methodQueue(delegate(){methodTwo( 3,4 );});
}

static void methodOne (int x,int y)
{

}

static void methodQueue (QueuedMethod parameter)
{
    parameter(); //run the method
    //...wait
    //...execute the parameter statement
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读