当前位置:首页 > 开发 > 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# protected

官方:只有在通过派生类类型发生访问时,基类的受保护成员在派生类中才是可访问的。 简单理解:...

C# 获取网页源代码

private static string GetHtml(str...

C# BackgroundWorker

1.概述BackgroundWorker是一个在 WinForms 应用程序中用于简化在后台线程执行...

C# double转为string并保留两位小数

在 C# 中,可以使用多种方式将 double 类型的数据转换为 string 类型并保留两位小数,...

c# Invalidate、Refresh、Refreshitem、Refreshitems的功能

Invalidate方法功能概述Invalidate方法主要用于使控件的特定区域(整个控件或部分区域...

C# Byte[]转为Bitmap

在 C# 中,可以使用System.Drawing命名空间下的相关类将byte[]类型的数据转换为B...