系统内置委托Action和func

时间:2023-03-10 03:12:50
系统内置委托Action和func
Action委托,
action是系统内置的委托,它可指向无返回值,没有参数的方法.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void print()
{
Console.WriteLine("hello world!");
}
static void doubleint(int t1, int t2)
{
Console.WriteLine(t1+t2);
}
static void web(int c)
{ }
static void Main(string[] args)
{
Action a = print;
Action<int> b=web;//这个泛型委托,可指向有一个参数的方法,根据泛型类来定
//系统会自动匹配据泛型选择的方法
Action<int, int> c = doubleint;
c(, );
Console.ReadKey();//可随意进行修改类型,个数,类型均可修改,最大可支持16个,
//不过参数类型要和委托类型一一对应
}
}
}

Func委托:

两个类

 static int function()
{
return ;
}
static int function2(string str)
{
Console.WriteLine(str);
return ;
}

主函数结果

Func<int> d = function;
Func<string, int> e = function2;
Console.WriteLine(d()+e("hello"));//func前面类型为参数类型,后面为带有out的返回值

func委托必须是指向有返回值的方法,和Aciton委托一样可有16个参数,但一定要有返回值

本人小白,如有不足,还请指正!->1399971898@qq.com