使用C#将方法传递为参数

使用C#将方法传递为参数,第1张

使用C#将方法传递为参数

您可以将.net
3.5中的Func委托用作RunTheMethod方法中的参数。Func委托允许您指定一个方法,该方法采用许多特定类型的参数并返回特定类型的单个参数。这是一个应该起作用的示例:

public class Class1{    public int Method1(string input)    {        //... do something        return 0;    }    public int Method2(string input)    {        //... do something different        return 1;    }    public bool RunTheMethod(Func<string, int> myMethodName)    {        //... do stuff        int i = myMethodName("My String");        //... do more stuff        return true;    }    public bool Test()    {        return RunTheMethod(Method1);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5559885.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存