delegate对于+=运算符的重载实际就是调用的combine方法吗?但是+=后面跟一个new *delegate也可以,跟一个方法名也可以,而combine要求的是一个delegate的实例啊......why?

解决方案 »

  1.   

    Delegate.Combine 方法 (Delegate, Delegate)
    http://msdn.microsoft.com/zh-cn/30cyx32c(VS.80).aspx委托可以简单近似理解为C++里的函数指针A+=B 
    等于
    A->d1
       d2
       d3
    最后加上
    A->d1
       d2
       d3
       B返回A无非最后添加一个方法的入口指针或新委托方法(一个指针)
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication18
    {
        public partial class Form1 : Form
        {
            delegate void ShowText(String S); // 定义函数指针原型
            public Form1()
            {
                InitializeComponent();
                ShowText P = DoShowText; // 定义一个函数指针指向函数,这样这个指针存的就是函数的入口地址
                  P("GO"); // 利用函数指针调用函数
             }
            void DoShowText(String Text)
            {
                MessageBox.Show(Text);
            }
        }
    }
      

  3.   

    另外更正一下
    委托可以简单近似理解为C++里的函数指针数组 
        ______
    A->|  d1  | 
        ______
       |  d2  |
        ______
       |  d3  |
        ______
       |  B   |
        ______
      

  4.   

    我主要想知道的是:为什么+=后面可以跟一个函数名?函数名会相应的隐式转换为一个delegate吗?
    比如:P+=DoShowText;可以P+=new ShowText(DoShowText);也可以
    而Combine()要求的是一个delegate,那么P+=DoShowText;这句里面函数名相应的隐式转换为一个ShowText了吗?
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication18
    {
        public partial class Form1 : Form
        {
            delegate void ShowText(String S); // 函数定义
            ShowText DelegateList = null; // 定义一个函数指针空列表
            public Form1()
            {
                InitializeComponent();
                
                MessageBox.Show("开始第一次调用");
                DelegateList += DoShowText; // 函数的入口地址直接加入委托列表,内部列表+1,内容存放函数的入口地址
                DelegateList("1个函数指针,所以现在输出1次"); // 利用函数指针调用函数            MessageBox.Show("开始第二次调用");
                ShowText P = new ShowText(DoShowText);
                DelegateList += P; // 函数指针加入委托列表,内部列表+1,内容存放函数指针的地址,而函数指针指向函数入口地址,所以其实等于指向函数的入口地址
                DelegateList("2个函数指针,所以现在输出2次"); // 利用函数指针调用函数
            }
            void DoShowText(String Text)
            {
                MessageBox.Show(Text);
            }
        }
    }
      

  6.   

    至于 new ShowText(DoShowText); 产生的函数指针是指向原有函数的还是指向改函数的复制品就不知道了