title: Why is it that, in C#, referencing a variable from a function argument is faster than from a private property?
body: <p>It may be the case that my hardware is the culprit, but during testing, I've found that:</p>
  
  <pre><code>void SomeFunction(AType ofThing) {
      DoSomething(ofThing);
  }
  </code></pre>
  
  <p>...is faster than:</p>
  
  <pre><code>private AType _ofThing;
  void SomeFunction() {
      DoSomething(_ofThing);
  }
  </code></pre>
  
  <p>I believe it has to do with how the compiler translates this to CIL. Could anyone please explain, specifically, why does this happen?</p>
  
  <p>Here's some code where it happens:</p>
  
  <pre><code>public void TestMethod1()
  {
  
      var stopwatch = new Stopwatch();
  
      var r = new int[] { 1, 2, 3, 4, 5 };
      var i = 0;
      stopwatch.Start();
      while (i &lt; 1000000)
      {
          DoSomething(r);
          i++;
      }
  
      stopwatch.Stop();
  
      Console.WriteLine(stopwatch.ElapsedMilliseconds);
  
      i = 0;
      stopwatch.Restart();
      while (i &lt; 1000000)
      {
          DoSomething();
          i++;
      }
  
      stopwatch.Stop();
  
      Console.WriteLine(stopwatch.ElapsedMilliseconds);
  }
  
  private void DoSomething(int[] arg1)
  {
      var r = arg1[0] * arg1[1] * arg1[2] * arg1[3] * arg1[4];
  }
  
  private int[] _arg1 = new [] { 1, 2, 3, 4, 5 };
  private void DoSomething()
  {
      var r = _arg1[0] * _arg1[1] * _arg1[2] * _arg1[3] * _arg1[4];
  }
  </code></pre>
  
  <p>In my case it is 2.5x slower to use a private property.</p>