title: C# Action Variable Scope and Null References
body: <p>I can declare an Action and pass it a method on an instance object which modifies that object's internal state, then call the method and see that the private instance variables have changed. But if I create the Action, then null out the object it has a reference to a method on I don't get a null reference exception when I invoke the Action, but obviously I do if I try to access the object.</p>
  
  <p>How can it be changing anything if there is nothing there to change?</p>
  
  <p>A contrived example where I create a builder object which has a public void BuildWall method which just increments the number of walls it has built (a private integer variable). Why can I call buildWallAction after I have set builder to null or else how is the Action modifying the object if the object doesn't need to exist in order to call the action?</p>
  
  <p>Thanks in advance :)</p>
  
  <pre><code> class Program
  {
      static void Main(string[] args)
      {
          Builder builder = new Builder("Jim");
          Console.WriteLine(builder.ToString());
          Action buildWallAction = builder.BuildWall;
          builder = null; //no more builder to change via BuildWall()
          //starts work
  
          buildWallAction();  //these calls modify _wallsBuilt on the builder object 
          buildWallAction();  //but if we set it to null we can still call them just fine
  
          Console.WriteLine(builder.GetBuildingStatus()); //we will only get an exception here if builder is null
          Console.ReadKey();
      }
  }
  
  public class Builder
  {
      private string _name;
      private int _wallsBuilt;
      public Builder(string name)
      {
          _name = name;
          _wallsBuilt = 0;
      }
  
      public void BuildWall()
      {
          _wallsBuilt++;
      }
  
      public string GetBuildingStatus()
      {
          string msg = $"{_name} has built {_wallsBuilt} walls.";
          return msg;
      }
  }
  </code></pre>