C#委托的基本使用流程

C#委托的基本使用流程

【1】声明委托(方法的原型)
【2】根据委托编写具体方法
【3】定义委托变量
【4】将具体方法和委托变量关联
【5】使用委托变量

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 委托的使用01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }


        //【2】根据委托编写具体方法
        static void Lecture1()
        {
            Console.WriteLine("我们正在学习C#委托技术!");
        }
        static void Lecture11()
        {
            Console.WriteLine("我们正在学习WPF开发技术!");
        }
        static void Lecture111()
        {
            Console.WriteLine("我们正在学习项目实战开发技术!");
        }


        static string Lecture2(int count, string course)
        {
            return $" {course}  课时  {count}";
        }
        static string Lecture22(int count, string course)
        {
            return $" {course}  课时  {count}";
        }

        /// <summary>
        /// 使用委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //【3】定义委托变量
            LectureDelegate1 lecture1 = null;

            //【4】将具体方法和委托变量关联
            lecture1 = Lecture1;

            lecture1 += Lecture11;
            lecture1 += Lecture111;


            //【5】使用委托变量
            lecture1();

            // lecture1.Invoke(); //委托特有的Invoke();
            Console.WriteLine("************************************");
            lecture1 -= Lecture1;
            lecture1();
            Console.WriteLine("************************************");

            LectureDelegate2 lecture2 = Lecture2;
            Console.WriteLine(lecture2(200, ".NET高级编程"));

        }
    }

    //【1】声明委托(方法的原型)
    public delegate void LectureDelegate1();

    public delegate string LectureDelegate2(int num, string content);
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值