zoukankan      html  css  js  c++  java
  • 系统提供的Fun委托和Action委托

    Action类型委托的方法可以有0-16个参数,但不能有返回值。

    Func类型委托的方法可以有参数必须有返回值。

    Actoin使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 事件
    {
        class Program
        {
            static void Main(string[] args)
            {
                Action<int> del = m2;//委托的函数有一个参数
                del(10);
                Action del1 = m1;//委托的函数没有参数
                del1();
                Action<int, int> del2 = m3;//委托的函数有两个参数
                del2(10, 20);
                Console.ReadKey();
            }
            static  void m1()
            {
                Console.WriteLine("m1");
            }
            static void m2(int a)
            {
                Console.WriteLine(a);
            }
            static void m3(int a,int b)
            {
                Console.WriteLine("{0},{1}",a,b);
            }
        }
    }

    Func<参数,返回值>委托的使用  必须有返回值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 事件
    {
        class Program
        {
            static void Main(string[] args)
            {
               
                Func<int> fun = m1;//委托一个 无参数有int类型返回值的方法
                fun();
                Func<int,string> fun1 = m2;//委托一个有string类型返回值,int参数的方法
                fun1(29);
                Console.ReadKey();
            }
            static  int  m1()
            {
               
                return 10;
            }
            static string  m2(int a)
            {
               
                return "string";
            }
           
        }
    }
  • 相关阅读:
    Leetcode 217 存在重复
    Leetcode 125验证回文串
    HTML标签
    Having dreams is what makes life tolerable.
    Database数据库——MySQL简述
    Python实践之路8——选课系统
    Python学习之路16——线程、进程和协程
    Python实践之路7——计算器
    Python学习之路14——Socket
    Python学习之路13——异常处理
  • 原文地址:https://www.cnblogs.com/zhangyang4674/p/11469624.html
Copyright © 2011-2022 走看看