title: Performance difference in empty array initialization
body: <p>Recently, I've been working in performance/memory optimization and have stucked in the empty array initialization which uses generic method to initialize empty array:</p>
  
  <p><strong>Code implementation of generic empty array class:</strong> </p>
  
  <pre><code>    public static class EmptyArray&lt;T&gt;
      {
          public static readonly T[] Instance;
  
          static EmptyArray()
          {
              Instance = new T[0];
          }
      }
  </code></pre>
  
  <p>So, whenever creating empty array of any type,it has been called as like:</p>
  
  <pre><code>var emptyStringArray = EmptyArray&lt;string&gt;.Instance;
  </code></pre>
  
  <p>Such empty array declaration has been done in many places of codebase. I am confused how would it be differs in performance while using :</p>
  
  <pre><code>var emptyStringArray = new string[0];
  </code></pre>
  
  <p>I've asked to above code author and he has replied me :</p>
  
  <blockquote>
    <p>Basically, all empty arrays are readonly, and are equal to one
    another, which means that you can use the same instance (which will be
    created lazily on demand during run-time)… That should reduce the
    total number of allocations, reduce memory usage and GC pressure, and
    should result in some improvement</p>
  </blockquote>
  
  <p>Still, I am not able to understand how would <code>EmptyArray</code> <code>Instance</code> boost the performance in array declaration.</p>
  
  <p>Is there any performance difference in code using following approach :</p>
  
  <p><strong>1st Approach :</strong> </p>
  
  <pre><code>var emptyStrArr = EmptyArray&lt;string&gt;.Instance;
  var emptyFooArr = EmptyArray&lt;Foo&gt;.Instance;
  var emptyBarArr = EmptyArray&lt;Bar&gt;.Instance;
  </code></pre>
  
  <p><strong>2nd Approach :</strong></p>
  
  <pre><code>var emptyStrArr = new string[0];
  var emptyFooArr = new Foo[0];
  var emptyBarArr = new Bar[0];
  </code></pre>