title: Is there a way to force static fields to be initialized in C#?
body: <p>Consider the following code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Program
  {
      static Program() {
          Program.program1.Value = 5;
      }
  
      static List&lt;Program&gt; values = new List&lt;Program&gt;();
      int value;
      int Value
      {
          get { return value; }
          set { 
              this.value = value;
              Program.values.Add(this);
          }
      }
  
      static Program program1 = new Program { value = 1 };
      static Program program2 = new Program { value = 2 };
      static Program program3 = new Program { value = 3 };
  
      static void Main(string[] args)
      {
          if (Program.values.Count == 0) Console.WriteLine("Empty");
          foreach (var value in Program.values)
              Console.WriteLine(value.Value);
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>It prints only the number 5, and if removed the code in the static constructor, it prints "Empty".</p>
  
  <p>Is there a way to force static fields to be initialized even whether not used yet?</p>
  
  <p>I need to have a static property named Values with returns all instances of the referred type.</p>
  
  <p>I tried some variations of this code and some works for some types but doesn't for others.</p>
  
  <p>EDIT: THE SAMPLE ABOVE IS BROKEN, TRY THIS ONE:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Subclass&lt;T&gt; {
      static Subclass()
      {
          Values = new List&lt;Subclass&lt;T&gt;&gt;();
      }
      public Subclass()
      {
          if (!Values.Any(i =&gt; i.Value.Equals(this.Value)))
          {
              Values.Add(this);
          } 
      }
  
      public T Value { get; set; }
  
      public static List&lt;Subclass&lt;T&gt;&gt; Values { get; private set; }
  }
  
  class Superclass : Subclass&lt;int&gt;
  {
      public static Superclass SuperclassA1 = new Superclass { Value = 1 };
      public static Superclass SuperclassA2 = new Superclass { Value = 2 };
      public static Superclass SuperclassA3 = new Superclass { Value = 3 };
      public static Superclass SuperclassA4 = new Superclass { Value = 4 }; 
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          //Console.WriteLine(Superclass.SuperclassA1); //UNCOMMENT THIS LINE AND IT WORKS
          foreach (var value in Superclass.Values)
          {
              Console.WriteLine(value.Value);
          }
          Console.ReadKey();
      }
  }
  </code></pre>