title: Why won't this static variable increment when using generics?
body: <p>I need a certain class to contain a static member that keeps track of everytime an instance of that class is instantiated, essentially so that each instance of the class has a unique index. It works with a non-generic class but this generic implementation fails whenever the type T differs between instances:</p>
  
  <pre><code>class A&lt;T&gt;
  {
     private static int counter;
  
     private static int Counter {
         get { 
            Increment(); 
            return counter; 
         }
     }
  
     private static void Increment() {
         counter++; 
     }
  
     public int Index; 
  
     public A()
     {
         this.Index = Counter; // using A&lt;T&gt;.Counter makes no difference
  
         Console.WriteLine(this.Index);      
     }
  }
  
  
  class Program
  {
      static void Main(string[] args)
      {
          var a = new A&lt;string&gt;();
          var b = new A&lt;string&gt;(); 
          var c = new A&lt;string&gt;();
          var d = new A&lt;int&gt;(); 
      }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <p>1</p>
  
  <p>2</p>
  
  <p>3</p>
  
  <p>1</p>
  
  <p>As soon as the type T switches to int instead of string, the counter resets. </p>
  
  <p>Does this fail by design, and if so what is the reason or how can I get around it? Or is it a bug? It makes sense to some degree because the type T, being generic, is in the class declaration, but..</p>