C# 分配与创建新代理

C# 分配与创建新代理,c#,C#,在中,我在设置委托变量时发现了一个奇怪的表达式: 为什么使用创建新对象的语法: myDelegate = new AddListItem(AddListItemMethod); 而不是分配 myDelegate = AddListItemMethod; 这些表达方式有什么区别?表达式newaddListItem(…)的含义是什么 全部代码: public class MyFormControl : Form { public delegate void AddListIt

在中,我在设置委托变量时发现了一个奇怪的表达式:

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

myDelegate = new AddListItem(AddListItemMethod);
而不是分配

myDelegate = AddListItemMethod;
这些表达方式有什么区别?表达式
newaddListItem(…)
的含义是什么

全部代码:

 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);
      }
   }
公共类MyFormControl:表单
{
公共委托void AddListItem();
公共addListItemMyDelegate;
私人按钮myButton;
私有线程读取;
私有列表框myListBox;
公共MyFormControl()
{
myButton=新按钮();
myListBox=新列表框();
myButton.Location=新点(72160);
myButton.Size=新尺寸(152,32);
myButton.TabIndex=1;
myButton.Text=“在列表框中添加项目”;
myButton.Click+=新建事件处理程序(按钮单击);
myListBox.Location=新点(48,32);
myListBox.Name=“myListBox”;
myListBox.Size=新尺寸(200,95);
myListBox.TabIndex=2;
ClientSize=新尺寸(292273);
AddRange(新控件[]{myListBox,myButton});
Text=“'Control_Invoke'示例”;
myDelegate=新的AddListItem(AddListItemMethod);
}
静态void Main()
{
MyFormControl myForm=新建MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod()
{
字符串myItem;

对于(inti=1;i没有差异


新的
语法是很久以前就需要的,从那时起,它们使c#编译器足够聪明,可以在您将方法分配给委托变量时为您创建此代码。

没有区别

新的
语法是很久以前就需要的,从那时起,他们使c#编译器足够聪明,可以在为委托变量分配方法时为您创建此代码。

无,请参阅(右窗格,IL代码)。第二个转换为第一个。无,请参阅(右窗格,IL代码)第二个转换为第一个。