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(50, 50);
- Console.ReadKey();// 可随意进行修改类型, 个数, 类型均可修改, 最大可支持 16 个,
- // 不过参数类型要和委托类型一一对应
- }
- }
- }
Func 委托:
两个类
- static int function()
- {
- return 1;
- }
- static int function2(string str)
- {
- Console.WriteLine(str);
- return 100;
- }
主函数结果
- Func<int> d = function;
- Func<string, int> e = function2;
- Console.WriteLine(d()+e("hello"));//func 前面类型为参数类型, 后面为带有 out 的返回值
func 委托必须是指向有返回值的方法, 和 Aciton 委托一样可有 16 个参数, 但一定要有返回值
本人小白, 如有不足, 还请指正!->[email protected]
来源: http://www.bubuko.com/infodetail-3297534.html