title: Does C# have zero-cost abstractions?
body: <p>I normally program in languages that have the concept of zero-cost abstractions like C++ and Rust.</p>
  
  <p>Currently I'm working in a project that uses C# language. So I was wondering if I can safely create abstractions and higher level code without compromising the performance.</p>
  
  <p>Is that possible in C# or for performance critical code I should just do as low level code as possible?</p>
  
  <p>Just as an example I encountered in my code (don't focus too much on this example, my question is more high level), I needed a function that would return multiple values, for that, my first approach was to use a tuple, so something like this:</p>
  
  <pre><code>public (int, int, float) Function();
  </code></pre>
  
  <p>or Abstract this tuple into a struct:</p>
  
  <pre><code>public struct Abstraction { int value1; int value2; float value3; };
  
  public Abstraction Function();
  </code></pre>
  
  <p>What I expected is that the compiler would optimize the <code>Tuple</code> or the <code>Abstraction struct</code> away and simply use the primitive values directly. But what I found is that writing the code using <code>out</code> parameters would improve performance:</p>
  
  <pre><code>public void Function(out int value1, out int value2, out float value3);
  </code></pre>
  
  <p>I'm guessing the reason is because in the <code>out</code> function, there is no <code>Tuple</code> or <code>Abstraction struct</code> creation.</p>
  
  <p>The problem with the <code>out</code> function version is that I really hate to use parameters as return values, since it seems more like a <em>hack</em> to a language limitation.</p>
  
  <p>So, in the end I'm not sure if I'm just not using the correct configuration so the JIT could use zero-cost abstraction or this is simply not possible or not guaranteed in C#. </p>