C# 委托 delegate 入门

官网示例:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ConsoleApp1
{
    // Declare delegate -- defines required signature:
    delegate double MathAction(double num);

    class Program
    {
        // Regular method that matches signature:
        static double Double(double input)
        {
            return input * 2;
        }

        static void Main(string[] args)
        {
            //双倍
            MathAction ma = Double;

            // Invoke delegate ma:
            double multByTwo = ma(4.5);
            Console.WriteLine("multByTwo: {0}", multByTwo);

            //矩形面积
            MathAction ma2 = delegate (double input)
            {
                return input * input;
            };

            double square = ma2(5);
            Console.WriteLine("square: {0}", square);

            //正方体体积
            MathAction ma3 = s => s * s * s;
            double cube = ma3(4.375);

            Console.WriteLine("cube: {0}", cube);
            Console.Read();
        }
    }
}

结果:


由示例可见:

官方示例写了三种实现委托的方式

1.将方法赋给 dekegate

需要先定义一个delegate 对象

        private static dekegate double  GetDou(double dou);

        GetDou md= 方法名;  //这里的方法要求参数类型和委托相同

        md(4.5);

2.直接写方法体的方式

需要先定义一个delegate 对象

    private static dekegate double  GetDou(double dou);

    GetDou  md =delegate(double input)

    {

            return input*input

      };

    var res = md(2);

结果:4

3.运用拉姆达表达式直接写逻辑

需要先定义一个delegate 对象

    private static dekegate double  GetDou(double dou)

    GetDou  ma = s=>s*s*s;

    double res = ma(2);

    结果:8



看了其他博主:

写法:

1、委托 委托名=new 委托(会调用的方法名); 委托名(参数);

2、委托 委托名 =会调用的方法名; 委托名(参数);

3、匿名方法

委托 委托名=delegate(参数){会调用的方法体};委托名(参数);

4、拉姆达表达式

委托 委托名=((参数1,。。参数n)=>{会调用的方法体});委托名(参数);

5、用Action<T>和Func<T>,第一个无返回值

Func<参数1, 参数2, 返回值> 委托名= ((参数1,参数2) => {带返回值的方法体 });返回值=委托名(参数1,参数2);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using @delegate;
 
namespace @delegate
{
        public delegate int Call(int num1, int num2);
        class SimpleMath
        {
            // 乘法方法
            public static int Multiply(int num1, int num2)
            {
                return num1 * num2;
            }
 
            // 除法方法
            public int Divide(int num1, int num2)
            {
                return num1 / num2;
            }
        }
    class Test
    {
        static void Main(string[] args)
        {
            //--------------------第一种写法------------------------//
            //Call objCall = new Call(SimpleMath.Multiply);
            //Call objCall1 = new Call(new SimpleMath().Divide);
            //--------------------第二种写法------------------------//
            //Call objCall = SimpleMath.Multiply;
            //Call objCall1 = new SimpleMath().Divide;
            //--------------------第三种写法------------------------//
            //Call objCall = delegate(int a, int b)
            //{
            //    return a * b;
            //};
            //Call objCall1 = delegate(int a, int b)
            //{
            //    return a / b;
            //};
            //--------------------第四种写法------------------------//
            //Call objCall =((int a,int b)=> { return a*b;});
            //Call objCall1 = ((int a, int b) => { return a / b; });
            //--------------------第五种写法------------------------//
            Func<int, int, int> objCall = ((a, b) => { return a * b; });
            Func<int, int, int> objCall1 = ((a, b) => { return a / b; });
             Action<int, int> ob = ((a, b) => { Console.WriteLine(a * b); });
             ob(5, 3);
            //----------------------------------------------------//
             int result = objCall(5, 3);
             int result1 = objCall1(5, 3);
             System.Console.WriteLine("结果1为 {0},结果2为{1}", result,result1);
            Console.ReadKey();
        }
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值