当前位置:首页 > 开发 > C# > 正文内容

C# 将函数作为参数传递

C#2年前 (2023-07-06)
无参数    
static void Main(string[] args)
{
    B(A);
    Console.Read();
}
static void A()
{
    Console.WriteLine("无尽的华尔兹"); 
}
static void B(Action action)
{
    action.Invoke();
}



单参数    
static void Main(string[] args)
{
    B(A,"无尽的华尔兹");
    Console.Read();
}
static void A(string text)
{
    Console.WriteLine(text); 
}
static void B(Action<string> action,string text)
{
    action.Invoke(text);
}



多参数    
static void Main(string[] args)
{
    B(A, "无尽的华尔兹", 999);
    Console.Read();
}
static void A(string text, int num)
{
    Console.WriteLine(text + num.ToString());
}
static void B(Action<string, int> action, string text1, int num1)
{
    action.Invoke(text1, num1);
}









转载请注明出处。

本文链接:http://pythonopen.com/?id=292

相关文章

C# 模拟按键

方法1SendKeys.SendWait("123{TAB}abc");&nbs...

C# 可空参数

using System; using System.Runtime.Inte...

C# 跳出foreach循环

在 C# 中,如果你想在 foreach 循环内部提前跳出当前这一轮循环,继续执行下一轮循环,可以使...

C# BackgroundWorker的例子

以下是一个使用 BackgroundWorker 组件在 C# 中实现后台执行任务,同时在主线程更新...

C# decimal

概述在 C# 中,decimal是一种数据类型,用于表示高精度的十进制数值。它主要用于需要精确计算的...

C# ref 和out

ref关键字概念:ref是 C# 中的一个关键字,用于按引用传递参数。当在方法调用中使用ref关键字...