c# 学习笔记 - 委托(Delegate)

文章目录

    • [1. 委托](#1. 委托)
      • [1.1 委托概述](#1.1 委托概述)
      • [1.2 委托使用](#1.2 委托使用)
      • [1.3 委托的传播](#1.3 委托的传播)
    • [2. 匿名方法](#2. 匿名方法)
      • [2.1 匿名方法概述](#2.1 匿名方法概述)
      • [2.2 匿名方法](#2.2 匿名方法)

1. 委托

1.1 委托概述

**  委托简介**

  1. 委托就是对方法的引用,可以理解为例如整型变量的容器可以存储整形数据,委托就是某种方法的容器,可以用来存储这一类的方法。
csharp 复制代码
// 声明一个public访问修饰符的 (具有输入字符串类型 + 返回整形数据类型) 的委托
public delegate int MyDelegate(string s); // 委托的模板
  1. 委托分为两步,第一步需要声明这种委托(即定义此类委托是存储什么样子的方法的容器),当我们声明完成委托就相当于拥有了这个容器的模板,其第二步就是实例化委托,就是相当于我们往这个容器当中加入我们想要的方法完成其实例化。
csharp 复制代码
static int way1(string s){ // 定义符合委托模板的方法
    Console.WriteLine("way1: 传入的字符串为: " + s + ", 随后我即将返回整形数字1");
    return 1;
}

MyDelegate m1; // 通过委托模板实例化委托的容器
m1 = way1; // 往这个委托的容器当中放入方法
MyDelegate m1 = new MyDelegate(way1); // 等效于上述两个步骤之和

1.2 委托使用

csharp 复制代码
using System;

namespace DelegateAppl {
    class TestDelegate {
        // 声明一个public访问修饰符的 (具有输入字符串类型 + 返回整形数据类型) 的委托模板
        public delegate int MyDelegate(string s);

        static int way1(string s){
            Console.WriteLine("way1: 传入的字符串为: " + s + ", 随后我即将返回整形数字1");
            return 1;
        }

        static void Main(){
            MyDelegate m1; // 通过委托模板获取委托的容器
            m1 = way1; // 往这个委托的容器当中放入方法

            m1("hi.."); // way1: 传入的字符串为: hi.., 随后我即将返回整形数字1
        }
    }
}

1.3 委托的传播

  1. 简而言之就是委托不仅仅可以存储一个方法,它可以按顺序存储多个方法(注意添加的顺序就是委托内部方法的执行顺序)
csharp 复制代码
using System;

namespace DelegateAppl {
    class TestDelegate {
        public delegate int MyDelegate(string s);

        static int way1(string s){
            Console.WriteLine("way1: 传入的字符串为: " + s + ", 随后我即将返回整形数字1");
            return 1;
        }

        static int way2(string s) {
            Console.WriteLine("way2: 传入的字符串为: " + s + ", 随后我即将返回整形数字2");
            return 2;
        }

        static void Main(){
            MyDelegate m1; 
            m1 = way1;
            m1 += way2; // 或者可以先实例化一个新的MyDelegate m2 = way2; 再用 m1 += m2; 

            m1("hi.."); 
        }
    }
}
/*
way1: 传入的字符串为: hi.., 随后我即将返回整形数字1
way2: 传入的字符串为: hi.., 随后我即将返回整形数字2
 */

2. 匿名方法

2.1 匿名方法概述

**  简要说明**

  1. 之前我们写委托的时候需要首先声明一个方法,再把这个方法名字给到委托对象去使用.
  2. 匿名方法就是省去方法名字,直接将这个方法的内部操作直接给委托对象(建议当此方法只需要引用一次的时候去使用,因为这个方法是没有方法名)
csharp 复制代码
public delegate int MyDelegate(string s);
MyDelegate m1 = delegate (string s) { // 匿名方法(省去方法的名字)
    Console.WriteLine(s);
    return 3;
};

2.2 匿名方法

csharp 复制代码
using System;

namespace DelegateAppl {
    class TestDelegate {
        public delegate int MyDelegate(string s);

        static void Main(){
            MyDelegate m1 = delegate (string s) {
                Console.WriteLine(s);
                return 3;
            };

            int num = m1("hello world.....");
            Console.WriteLine(num);
        }
    }
}
/*
hello world.....
3
 */
相关推荐
翻滚的小@强1 小时前
数据挖掘笔记:点到线段的距离计算
人工智能·笔记·数据挖掘
会思考的猴子1 小时前
UE5 PCG 笔记(二) Difference 节点
笔记·ue5
yuxb732 小时前
Linux 文本处理与 Shell 编程笔记:正则表达式、sed、awk 与变量脚本
linux·笔记·正则表达式
饕餮争锋5 小时前
设计模式笔记_行为型_访问者模式
笔记·设计模式·访问者模式
不羁。。7 小时前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
好望角雾眠11 小时前
第一阶段C#基础-10:集合(Arraylist,list,Dictionary等)
笔记·学习·c#
艾伦~耶格尔12 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
星仔编程12 小时前
python学习DAY46打卡
学习
大霞上仙12 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
yatingliu201914 小时前
HiveQL | 个人学习笔记
hive·笔记·sql·学习