代码之家  ›  专栏  ›  技术社区  ›  vico

分配vs创建新委托[重复]

c#
  •  -1
  • vico  · 技术社区  · 7 年前

    这个问题已经有了答案:

    sample in MSDN regarding Invoke 我在设置委托变量时发现了奇怪的表达式:

    为什么使用创建新对象的语法:

    myDelegate = new AddListItem(AddListItemMethod);
    

    而不是分配

    myDelegate = AddListItemMethod;
    

    这些表达式之间有什么区别?表达的意义是什么 new AddListItem(...) ?

    完整代码:

     public class MyFormControl : Form
       {
          public delegate void AddListItem();
          public AddListItem myDelegate;
          private Button myButton;
          private Thread myThread;
          private ListBox myListBox;
          public MyFormControl()
          {
             myButton = new Button();
             myListBox = new ListBox();
             myButton.Location = new Point(72, 160);
             myButton.Size = new Size(152, 32);
             myButton.TabIndex = 1;
             myButton.Text = "Add items in list box";
             myButton.Click += new EventHandler(Button_Click);
             myListBox.Location = new Point(48, 32);
             myListBox.Name = "myListBox";
             myListBox.Size = new Size(200, 95);
             myListBox.TabIndex = 2;
             ClientSize = new Size(292, 273);
             Controls.AddRange(new Control[] {myListBox,myButton});
             Text = " 'Control_Invoke' example";
             myDelegate = new AddListItem(AddListItemMethod);
          }
          static void Main()
          {
             MyFormControl myForm = new MyFormControl();
             myForm.ShowDialog();
          }
          public void AddListItemMethod()
          {
             String myItem;
             for(int i=1;i<6;i++)
             {
                myItem = "MyListItem" + i.ToString();
                myListBox.Items.Add(myItem);
                myListBox.Update();
                Thread.Sleep(300);
             }
          }
          private void Button_Click(object sender, EventArgs e)
          {
             myThread = new Thread(new ThreadStart(ThreadFunction));
             myThread.Start();
          }
          private void ThreadFunction()
          {
             MyThreadClass myThreadClassObject  = new MyThreadClass(this);
             myThreadClassObject.Run();
          }
       }
    
    // The following code assumes a 'ListBox' and a 'Button' control are added to a form, 
    // containing a delegate which encapsulates a method that adds items to the listbox.
    
       public class MyThreadClass
       {
          MyFormControl myFormControl1;
          public MyThreadClass(MyFormControl myForm)
          {
             myFormControl1 = myForm;
          }
    
          public void Run()
          {
             // Execute the specified delegate on the thread that owns
             // 'myFormControl1' control's underlying window handle.
             myFormControl1.Invoke(myFormControl1.myDelegate);
          }
       }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Rafal    7 年前

    没有区别。

    这个 new 语法是很久以前所需要的,从那时起,当您将方法赋给委托变量时,它们使C编译器足够聪明,可以为您创建此代码。