C#泛型委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03泛型委托
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 泛型委托
            //Action 系统自定义的委托用来保存无参数无返回值的方法
            //1.保存一个无参数无返回值的方法
            Action a1 = new Action(F1);
            a1();

            //Action<T> Action的泛型版本 用来保存有参数无返回值的方法
            //2.保存一个有参数无返回值的方法
            Action<string> a2 = new Action<string>(F2);
            a2("有参数无返回值的方法");

            Action<int, int> a22 = (x, y) => { Console.WriteLine(x + y); };
            a22(1, 2);

            //Func委托最后一个参数永远表达式返回值内容
            //3.保存有参数有返回值的方法
            Func<int, int, int, int> func1 = F3;
            int print = func1(1, 2, 3);
            Console.WriteLine(print);
            #endregion
          Console.WriteLine("=============================================================");

            #region 使用lambda表达式
            List<int> l1 = new List<int> { 1, 23, 4, 5, 656, 5, 75, 2, 323, 2, 456, 4, 2, 234, 645, 6 };

            //IEnumerable<int> ie =  l1.Where(condition);
            IEnumerable<int> ie = l1.Where(m => { return m > 6; });
            IEnumerable<int> ie2 = l1.SkipWhile(m => { return m > 10; });

            foreach (var item in ie)
            {
                Console.WriteLine(item);
            }
           Console.WriteLine("=============================================================");
            foreach (var item in ie2)
            {
                Console.WriteLine(item);
            }

            #endregion
            Console.ReadKey();
        }
        static bool condition(int x)
        {
            return x > 6;
        }

        static int F3(int x, int y, int z)
        {
            return x + y + z;
        }

        static void F2(string s1)
        {
            Console.WriteLine(s1);
        }

        static void F1()
        {
            Console.WriteLine("无参数无返回值的方法");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值