zoukankan      html  css  js  c++  java
  • Delegate委托

    1. 显式调用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        //定义委托
        public delegate int MethodDelegate(int x, int y);
        class Program
        {
            static void Main(string[] args)
            {
                //初始化委托方式一
                //MethodDelegate method = new MethodDelegate(Add);
                //初始化委托方式二
                MethodDelegate method = Add;
                Console.WriteLine(method(10, 20));  //30
                Console.ReadKey();
            }
            private static int Add(int x, int y)
            {
                return x + y;
            }
        }
    }
    View Code

    2. 匿名方法调用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        //定义委托
        public delegate int MethodDelegate(int x, int y);
        class Program
        {
            static void Main(string[] args)
            {
                //初始化委托方式一
                //MethodDelegate method = new MethodDelegate(delegate(int x, int y) { return x + y; });
                //初始化委托方式二
                MethodDelegate method = delegate(int x, int y)
                {
                    return x + y;
                };
                Console.WriteLine(method(10, 20));  //30
                Console.ReadKey();
            }
        }
    }
    View Code

    3. Lambda表达式调用(相比匿名方法写法更加简单,推荐使用这种方式)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        //定义委托
        public delegate int MethodDelegate(int x, int y);
        class Program
        {
            static void Main(string[] args)
            {
                //简单的用法
                MethodDelegate method1 = (int x, int y) => { return x + y; };
                MethodDelegate method2 = (x, y) => { return x + y; };
                MethodDelegate method3 = (x, y) => x + y;
                Console.WriteLine(method3(10, 20));  //30
    
                //复杂的用法
                MethodDelegate method4 = (x, y) =>
                {
                    if (x > y)
                    {
                        x = x * 10;
                        return x - y;
                    }
                    else
                    {
                        return x + y;
                    }
                };
                //调用方式1
                //Console.WriteLine(method4.Invoke(30, 10));//290
                //调用方式2
                Console.WriteLine(method4(30, 10));//290
                Console.ReadKey();
            }
        }
    }
    View Code
  • 相关阅读:
    Hbase王国游记之:Hbase客户端API初体验
    Hbase给初学者的“下马威”
    五分钟轻松了解Hbase面向列的存储
    JsonBuilder初出茅庐
    如何查看laravel门脸类包含方法的源码
    PHP常用数组函数
    Go语言strings包
    PHP获取远程http或ftp文件的md5值
    Git使用详细教程(9):git log
    PHP Iterator迭代对象属性
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12887532.html
Copyright © 2011-2022 走看看