What is the difference between lambdas and delegates in the .NET Framework?

后端 未结 17 1404
小蘑菇
小蘑菇 2020-12-12 10:45

I get asked this question a lot and I thought I\'d solicit some input on how to best describe the difference.

17条回答
  •  鱼传尺愫
    2020-12-12 11:15

    Heres an example I put up awhile on my lame blog. Say you wanted to update a label from a worker thread. I've got 4 examples of how to update that label from 1 to 50 using delegates, anon delegates and 2 types of lambdas.

     private void button2_Click(object sender, EventArgs e) 
         { 
             BackgroundWorker worker = new BackgroundWorker(); 
             worker.DoWork += new DoWorkEventHandler(worker_DoWork); 
             worker.RunWorkerAsync(); 
         } 
    
         private delegate void UpdateProgDelegate(int count); 
         private void UpdateText(int count) 
         { 
             if (this.lblTest.InvokeRequired) 
             { 
                 UpdateProgDelegate updateCallBack = new UpdateProgDelegate(UpdateText); 
                 this.Invoke(updateCallBack, new object[] { count }); 
             } 
             else 
             { 
                 lblTest.Text = count.ToString(); 
             } 
         } 
    
         void worker_DoWork(object sender, DoWorkEventArgs e) 
         {   
             /* Old Skool delegate usage.  See above for delegate and method definitions */ 
             for (int i = 0; i < 50; i++) 
             { 
                 UpdateText(i); 
                 Thread.Sleep(50); 
             } 
    
             // Anonymous Method 
             for (int i = 0; i < 50; i++) 
             { 
                 lblTest.Invoke((MethodInvoker)(delegate() 
                 { 
                     lblTest.Text = i.ToString(); 
                 })); 
                 Thread.Sleep(50); 
             } 
    
             /* Lambda using the new Func delegate. This lets us take in an int and 
              * return a string.  The last parameter is the return type. so 
              * So Func would take in an int and a string 
              * and return a double.  count is our int parameter.*/ 
             Func UpdateProgress = (count) => lblTest.Text = count.ToString(); 
             for (int i = 0; i < 50; i++) 
             { 
                 lblTest.Invoke(UpdateProgress, i); 
                 Thread.Sleep(50); 
             } 
    
             /* Finally we have a totally inline Lambda using the Action delegate 
              * Action is more or less the same as Func but it returns void. We could 
              * use it with parameters if we wanted to like this: 
              * Action UpdateProgress = (count) => lblT…*/ 
             for (int i = 0; i < 50; i++) 
             { 
                 lblTest.Invoke((Action)(() => lblTest.Text = i.ToString())); 
                 Thread.Sleep(50); 
             } 
         }
    

提交回复
热议问题