title: std::bind equivalent in C# or VB.NET
body: <p>I am in the process of refactoring "synchronous" code (i.e. uses Windows events to wait until some other thread finished doing something) to "asynchronous" code (using delegates to implement a callback mechanism).</p>
  
  <p>In the sync code, I sometimes have local variables that I need to use after the wait is over. When code like that goes async, those local variables are lost (the callback handler can't access them). I can store them as class attributes, but it feels wasteful.</p>
  
  <p>In C++, I use <code>std::bind</code> to work around this. I just add as many parameters as local variables needed to the callback handler, and bind them when I call the async method. For example, let's say that the async method callback receives an object of type <code>CallbackParam</code> and the caller uses two local variables of type <code>LocalA</code> and <code>LocalB</code>.</p>
  
  <pre><code>void AsyncClass::MethodWhichCallsAsyncMethod(){
      LocalA localVarA;
      LocalB localVarB;
      // OnAsyncMethodDone will need localVarA and localVarB, so we bind them
      AsyncMethod( std::bind( &amp;AsyncClass::OnAsyncMethodDone, this, std::placeholders::_1, localVarA, localVarB ) );
  }
  
  void AsynClass::AsyncMethod( std::function&lt;void(CallbackParam)&gt; callback ){
      CallbackParam result;
      //Compute result...
      if( callback )
          callback( result );
  }
  
  void AsyncClass::OnAsyncMethodDone( CallbackParam p, LocalA a, LocalB b ){
     //Do whatever needs to be done
  }
  </code></pre>
  
  <p>Is there some sort of equivalent to this in C# and VB.NET? Using delegates or something else?</p>
  
  <p><strong>UPDATE</strong>: For completeness' sake, here is the C# equivalent of my example based on @lasseespeholt's answer:</p>
  
  <pre><code>using System;
  
  public class AsyncClass {
  
          public void MethodWhichCallsAsyncMethod() {
              var a = new LocalA();
              var b = new LocalB();
              //Anonymous callback handler (equivalent to AsyncClass::OnAsyncMethodDone)
              Action&lt;CallbackParam&gt; callback = result =&gt; {
                  //Do what needs to be done; result, a and b can be accessed
              };
              AsyncMethod( callback );
          }
  
          private void AsyncMethod( Action&lt;CallbackParam&gt; callback ) {
              var result = new CallbackParam();
              //Compute result...
              if( callback != null )
                  callback( result );
          }
  }
  </code></pre>