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
  • 相关阅读:
    MFC新建菜单项
    java连接mysql
    装visio 2007遇到了1706错误,解决办法
    Oracle协议适配器错误解决办法
    powershell 开启开发人员仪表盘
    sharepoint stsadm 创建网站脚本
    网站安全修复笔记1
    sharepoint ribbon添加菜单
    解决 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值
    RDA实现SQL CE与SQL Server间数据存取
  • 原文地址:https://www.cnblogs.com/LuckyZLi/p/12887532.html
Copyright © 2011-2022 走看看