zoukankan      html  css  js  c++  java
  • c#中匿名函数lamb表达式

    c#中匿名函数lamb表达式

    实例一:(其实,这样都是些语法糖)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        //c#中的匿名函数
        //申明一委托
        delegate void Del();
        class Program
        {
    
            static void show()
            {
                Console.WriteLine("show ......");
            }
    
            static void Test()
            {
                Del d = new Del(show);
                d();
            }
            static void Test2()
            {
                //你可以这么写....
                Del d = delegate()
                {
                    Console.WriteLine("show......");
                };
                d();
    
                //你也可以这么写
                Del d1 = delegate
                {
                    Console.WriteLine("show.....");
                };
    
                d1();
                //你还可以这么写;这就是lamb表达式;
                Del d2 = () =>
                {
                    Console.WriteLine("show.....");
                };
                d2();
    
            }
            static void Main(string[] args)
            {
    
                Test2();
                Console.ReadLine();
    
            }
        }
    }

      有参数的lamb表达式:

     static void Test()
            {
                //你可以这么写
                Dele d = delegate(int j)
                {
                    Console.WriteLine(j);
                };
                d(12);
    
                Dele d1 = (j) => { Console.WriteLine(j); };
                d1(12);
                //这个就是有参参的lamb表达式;
            }
    
            static void Main(string[] args)
            {
                Test();
                Console.ReadLine();
    
            }

    顺便提一下c#中的Action Func Predicate;

     Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

    Action是无返回值的泛型委托。

         Action 表示无参,无返回值的委托

         Action<int,string> 表示有传入参数int,string无返回值的委托

         Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托

         Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托

         Action至少0个参数,至多16个参数,无返回值。

    Func是有返回值的泛型委托

       Func<int> 表示无参,返回值为int的委托

       Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

       Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

       Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托

       Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

     (4) predicate

       predicate 是返回bool型的泛型委托

       predicate<int> 表示传入参数为int 返回bool的委托

       Predicate有且只有一个参数,返回值固定为bool

    实例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;
    
    namespace ConsoleApplication2
    {
       
        delegate void Dele(int i);
        class Program
        {
            static void add(int i,int j)
            {
                Console.WriteLine(i+j);
            }
            static int sub(int i, int j)
            {
                return i - j;
            }
    
            static bool  isTrue(int i)
            {
                if (i > 0)
                    return true;
                else
                    return false;
            }
    
            static void Test()
            {
    
                List<Action<int,int>> list = new List<Action<int,int>>();
                list.Add(add);
                //调用方式一
                list[0](100,10);
                //最后一个参数是返回值;
                Func<int,int,int> func=sub;
                int result=func(100,10);
                Console.WriteLine(result);
    
                Predicate<int> p = isTrue;
                bool re = p(100);
                Console.WriteLine(re);
             
            }
            static void Test2()
            {
                //当然我们可以换一种方式写滴呀
                Action<int, int> action = delegate(int i, int j)
                {
                    Console.WriteLine(i+j);
                };
                action(100,10);
    
                Func<int, int, int> func = delegate(int i, int j)
                {
                    return i - j;
                };
                int result = func(100,10);
    
                Predicate<int> pre = delegate(int i)
                {
                    bool re=
                    i==0?true: false;
                    return re;
                };
    
            }
            static void Test3()
            {
                //当然我们也可以这样写滴呀
                Action<int, int> action = (i, j) => { Console.WriteLine(i + j); };
                action(100,10);
    
                Func<int,int,int> func=(i,j)=>{return i-j;};
                int result=func(100,10);
    
                Predicate<int> pre = (i) => { if (i == 0)return true; else return false; };
                bool val = pre(100);
    
            }
    
            static void Test4()
            {
                Thread thread = new Thread(delegate()
                {
                    Console.WriteLine("hahhahah....");
                });
                thread.Start();
    
                Thread thread2 = new Thread(() =>
                {
                    Console.WriteLine("hahah......");
                });
                thread2.Start();
               
            }
    
            static void Main(string[] args)
            {
                //其实,就相当于这样滴呀:   public delegate void Action<T>(T arg);
                Test();
                Console.ReadLine();
    
            }
        }
    }
  • 相关阅读:
    无人值守安装linux
    数组中只出现过一次的数字 牛客网 剑指Offer
    数组中出现次数超过一半的数字 牛客网 剑指Offer
    数据流中的中位数 牛客网 剑指Offer
    数字在排序数组中出现的次数 牛客网 剑指Offer
    数值的整数次方 牛客网 剑指Offer
    按之字形顺序打印二叉树 牛客网 剑指Offer
    把数组排成最小的数 牛客网 剑指Offer
    把字符串转换成整数 牛客网 剑指Offer
    把二叉树打印成多行 牛客网 剑指Offer
  • 原文地址:https://www.cnblogs.com/mc67/p/5648219.html
Copyright © 2011-2022 走看看