zoukankan      html  css  js  c++  java
  • 我爱学习第一天(委托)

    ---恢复内容开始---

    1.delegate

    一般用法

     delegate void WriteValue(string vale);//申明一个委托,参数是string类型,无返回值
            static void Main(string[] args)
            {
                WriteValue write = new WriteValue(WriteLine);//实例化委托
                write("喝水");//调用
                Console.ReadKey();
    
            }
    
            public static void WriteLine(string value) {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("第{0}次{1}", i, value);
    
                }
            }
    View Code

    匿名委托

     delegate void WriteValue(string vale);
            static void Main(string[] args)
            {
                WriteValue write = delegate (string value) { Console.Write(value); };
                write("喝水");
                Console.ReadKey();
    
            }
    View Code

    使用lambda表达式

    如需要传入多个参数,需要用"()"将参数包括起来,用","隔开.

      delegate void WriteValue(string vale,string value1);
            static void Main(string[] args)
            {
                WriteValue write = (value,value1) => { Console.Write(value);Console.Write(value1); };
                write("喝水","吃饭");
                Console.ReadKey();
    
            }
    View Code

    多播委托

     delegate void WriteValue(string value);
            static void Main(string[] args)
            {
                WriteValue value1 = new WriteValue(Write);
                value1 += new WriteValue(WriteLine);
                value1("s");
                Console.ReadKey();
    
            }
    
            public static  void WriteLine(string value) {
                Console.WriteLine("帮我倒杯卡布奇洛,谢谢");
            }
    
            public static void Write(object value) {
    
                Console.WriteLine("多加点糖,谢谢");
            }
    View Code

    2.Func<T,E>

    T表示参数类型,E表示返回值类型. Func<T1,T2,T3,E>表示有三个参数,类型分别为T1,T2,T3,返回值为E,以此类推.Func<E>表示没有参数,返回值类型为E.

    delegate void WriteValue(string value);
            static void Main(string[] args)
            {
                Func<string, int> func = Length;
    
                Console.WriteLine(func("哈哈哈"));
                Console.ReadKey();
    
            }
            public static int Length(string value) {
                return value.Length;
            }
    View Code

    3.Action<T>

    Action<T1,T2,T3>表示有三个参数,无返回值,以此类推.

    static void Main(string[] args)
            {
                Action<string, string> action = Length;
                action("吃饭饭", "喝水水");
                Console.ReadKey();
    
            }
            public static void Length(string value,string value1) {
                Console.WriteLine(value);
                Console.WriteLine(value1);
            }
    View Code

    4.Predicate<T>:表示定义一组条件并确定指定对象是否符合这些条件的方法。该委托返回的是一个bool类型的值,如果比较满足条件 .只能有一个参数.

     static void Main(string[] args)
            {
                Predicate<int> predicate = Max;
                Console.WriteLine(predicate(12).ToString());
                Console.ReadKey();
    
            }
            public static bool Max(int value) {
                return value > 0;
            }
    View Code

    ---恢复内容结束---

  • 相关阅读:
    Java中的阻塞队列
    大数据笔记
    物联网小笔记
    shell 笔记
    Redis笔记
    rabbitMQ笔记
    java.lang.NoClassDefFoundError: freemarker/template/Template
    分布式系列学习-事务处理
    免费无需破解xshell xftp下载
    idea maven模块变灰或者java文件夹非 Sources文件夹
  • 原文地址:https://www.cnblogs.com/lzyqq/p/11309461.html
Copyright © 2011-2022 走看看