04-我对委托的初步认识

1、最基本的委托,就是一个可以存储方法的类型

案例:把一个方法赋值给一个委托类型的变量

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _01_委托基本
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //MyDelegate myDelegate = M1;//2.委托实例化
13             MyDelegate myDelegate = new MyDelegate(M1);//与上面的语句效果相同
14             myDelegate();//2.委托调用
15             Console.ReadKey();
16         }
17 
18         static void M1()
19         {
20             Console.WriteLine("当前时间是:{0}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
21         }
22     }
23 
24     delegate void MyDelegate();//声明一个无参数无返回值类型的委托
25 }

2、当一个方法想要作为参数传递时,使用委托:

示例代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ClsLibTest
 7 {
 8     public class Class1
 9     {
10         public delegate void MyDelegate();
11 
12         public void GetTime(MyDelegate WriteTime)
13         {
14             Console.WriteLine("======================");
16             WriteTime();
18             Console.WriteLine("======================");
19         }
20     }
21 }

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClsLibTest;

namespace _02_委托
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.GetTime(M1);
            Console.ReadKey();
        }

        static void M1()
        {
            Console.WriteLine("你好,系统当前时间是:{0}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }
}

 3、用委托实现窗体之间的回传值问题:

示例:

窗体1代码:

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

namespace 委托窗体之间回传值1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Update = UpdateText;//把UpdateText方法存放到委托变量中,
            f2.Show();
        }

        private void UpdateText(string text)
        {
            this.textBox1.Text = text;
        }
    }
}


窗体2代码:

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

namespace 委托窗体之间回传值1
{
    public delegate void Mydegate(string msg);

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Mydegate Update;//定义一个委托变量,用于存储方法

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            string input = this.textBox1.Text;//获得用户输入的文本

            if (Update != null)
            {
                Update(input);//调用委托方法
            }

        }
    } 
}

 

原文地址:https://www.cnblogs.com/zy-style/p/3279606.html