title: C# Asynchronous TCP and garbage collection of its closure referenced instance?
body: <p>Just wondering in case of Asynchronous TCP or other EAP pattern, if the success handler has a reference of <code>this</code>, e.g. <code>this.state</code>, in theory, there is a reference to current instance, as <code>this</code> is kept in some generated object scope by the closure nature. Thus the instance itself should not be garbage collected even the scope which create the instance is finished executing? </p>
  
  <p>My code is similar to following:</p>
  
  <pre><code> public class ATcpClient
      {
          private ATcpState state = null;
  
          private void Receive()
          {
              // create the callback here, in order to use in dynamic
              AsyncCallback ReceiveCallback = delegate(IAsyncResult ar)
                  {
                      try
                      {
                          // Read data from the remote device.
                          this.state.BytesReceived = this.state.Socket.EndReceive(ar);
  
                      }
                      catch (Exception e)
                      {
                          // ...
  
                      }
                  };
  
              try
              {
                      this.state.Socket.BeginReceive(this.state.Buffer, 0, this.state.BufferSize, 0,
                          ReceiveCallback, null);
              }
              catch (Exception e)
              {
                  // ...
                  // ...
              }
          }
  }
  </code></pre>
  
  <p>code which execute it can look like:</p>
  
  <pre><code>public void DoExecuteCode()
  {
      new ATcpClient().Receive();
  }
  </code></pre>
  
  <p>Would the instance been GC which cause the Receive() to fail as a whole?</p>