title: Which static class initializes first?
body: <p>Which static class initializes first if we have one more static classes in our project?</p>
  
  <p>For example: Below code gives null exception.</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              First.Write();
              Second.Write();
          }
      }
      static class First
      {
          public static int[] firstArray = new int[20];
          public static int[] secondArray = Second.secondArray;
          public static void Write()
          {
              Console.WriteLine(firstArray.ToString());
              Console.WriteLine(secondArray.ToString());
          }
      }
      static class Second
      {
          public static int[] firstArray = First.firstArray;
          public static int[] secondArray = new int[30];
          public static void Write()
          {
              Console.WriteLine(firstArray.ToString());
              Console.WriteLine(secondArray.ToString());
          }
      }
  </code></pre>
  
  <p>If you pay attention, you will see that if <code>First</code> class will initialize itself so <code>secondArray</code> field of <code>Second</code> would be null. But if <code>Second</code> class would initialize first so <code>Second</code> class <code>firstArray</code> would be null. I am trying to tell that which initialize first makes different results.</p>
  
  <p>I think that it is abstract question about my project. I encounter it while trying to understand why I am getting unexpected results.</p>