title: The performance cost to using ref instead of returning same types?
body: <p>Hi this is something that's really bothering me and I'm hoping someone has an answer for me. I've been reading about <code>ref</code> (and <code>out</code>) and I'm trying to figure out if I'm slowing down my code using <code>ref</code>s. Commonly I will replace something like:</p>
  
  <pre><code>int AddToInt(int original, int add){ return original+add; }
  </code></pre>
  
  <p>with</p>
  
  <pre><code>void AddToInt(ref int original, int add){ original+=add; } // 1st parameter gets the result
  </code></pre>
  
  <p>because to my eyes this</p>
  
  <pre><code>AddToInt(ref _value, _add);
  </code></pre>
  
  <p>is easier to read AND code than this</p>
  
  <pre><code>_value = AddToInt(_value, _add);
  </code></pre>
  
  <p>I know precisely what I'm doing on the code using <code>ref</code>, as opposed to returning  a value. However, performance is something I take seriously, and apparently dereferencing and cleanup is a lot slower when you use refs.</p>
  
  <p>What I'd like to know is <em>why</em> every post I read says there is very few places you would typically pass a <code>ref</code> (I know the examples are contrived, but I hope you get the idea), when it seems to me that the <code>ref</code> example is smaller, cleaner and more exact.</p>
  
  <p>I'd also love to know why <code>ref</code> really is slower than returning a value type - to me it would seem to me, if I was going to edit the function value a lot before returning it, that it would be quicker to reference the actual variable to edit it as opposed to an instance of that variable shortly before it gets cleaned from memory.</p>