zoukankan      html  css  js  c++  java
  • 委托(Delegate)简介

    1.什么是委托?是用来干什么的?

    委托是一个,它定义了方法的类型,使得可以将定义的方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。

    委托保存的是对函数(function)的引用,即保存对存储在托管堆(managed heap)中的对象的引用,不是实际值

     

    2.代码定义格式

    在C#中使用delegate关键字定义委托。

    [public/private/protect]修饰符 delegate [string/int/void]类型 name方法名();

    如:public delegate void student();   private delegate string ClassName();

     

    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 WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

           private delegate void show(string msg);

     

            public Form1()

            {

                InitializeComponent();

            }

            private void Form1_Load(object sender, EventArgs e)

            {

               show show = new show(message);

               show("aaa");

            }

            private void message(string msg)

            {

                MessageBox.Show(msg);

            }

        }

    }

    委托可以动态的与任何方法关联(但必须保证关联的方法与委托定义的方法类型相同、参数相同),上面的例子里就是将委托与message方法进行了关联。

    委托是对方法的包装。实现了对象与方法的隔离,增强了拓展性。

     

    4.简单用例2

    本示例演示组合委托。委托对象的一个有用属性是,它们可以“+”运算符来组合。组合的委托依次调用组成它的两个委托。只可组合相同类型的委托,并且委托类型必须具有 void 返回值。“-”运算符可用来从组合的委托移除组件委托。

    using System;

    delegate void MyDelegate(string s);

    class MyClass

    {

        public static void Hello(string s)

        {

            Console.WriteLine("  Hello, {0}!", s);

        }

        public static void Goodbye(string s)

        {

            Console.WriteLine("  Goodbye, {0}!", s);

        }

        public static void Main()

        {

           MyDelegate a, b, c, d;

           a = new MyDelegate(Hello);

           b = new MyDelegate(Goodbye);

           c = a + b;

           d = c - a;

            Console.WriteLine("Invoking delegate a:");

            a("A");

            Console.WriteLine("Invoking delegate b:");

            b("B");

            Console.WriteLine("Invoking delegate c:");

            c("C");

            Console.WriteLine("Invoking delegate d:");

            d("D");

        }

    }

    输出结果:

    Invoking delegate a:

      Hello, A!

    Invoking delegate b:

      Goodbye, B!

    Invoking delegate c:

      Hello, C!

      Goodbye, C!

    Invoking delegate d:

      Goodbye, D!

     

    5.简单用例3

    本实例展示泛型委托,即参数的类型不确定,以达到更高的灵活性:

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace Test

    {

       public delegate string ProcessDelegate<T,S>(T s1, S s2);

     

        public class Program

        {

            static void Main(string[] args)

            {

               ProcessDelegate<string,int> pd = new ProcessDelegate<string,int>(new Test().Process);

                Console.WriteLine(pd("Text1", 100));

            }

        }

     

        public class Test

        {

           public string Process(string s1,int s2)

            {

                return s1 + s2;

            }

        }

    }

      输出的结果就是:

      Text1100

     

    委托,其实就是对方法提供一个模板、一个规范、一个定义,就好比接口是对类得抽象,委托可以理解为对方法的抽象。从而实现对方法的封装。

    委托一个最主要的用途,就是事件。也就是说委托常常与事件联系在一起。

    在下一博文中,我将介绍如何利用委托来自定义事件。

  • 相关阅读:
    Elasticsearch教程(一)简介与安装
    Java注解
    easyui tree后台传json处理问题
    jquery-ui-bootstrap动态添加和删除标签页封装【效果更炫】
    springmvc+ztree v3实现类似表单回显功能
    org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案
    MVC之重定向
    MVC5之路由机制
    SQL技术内幕四
    SQL技术内幕三
  • 原文地址:https://www.cnblogs.com/opop/p/5276543.html
Copyright © 2011-2022 走看看