title: Making a generic Class IEnumerable
body: <p>I have a generic class which uses a type parameter </p>
  
  <pre><code>public class CustomClass&lt;T&gt; 
  </code></pre>
  
  <p>and I am using it with an <code>ObservableCollection&lt;someClass&gt;</code> Type. All I want is to make this class implement the IEnumerable interface, so I did the following:</p>
  
  <pre><code>public class CustomClass&lt;T&gt; : IEnumerable
  
  #region Variable Declarations
   ...
  #endregion
  
  #region Constructor and CustomClass&lt;T&gt; properties and methods
   ...
  #endregion
  
  #region Here I add the code for IEnumerable to work
  
  private T theObservableCollection
      {
          get
          {
              if (typeof(T) == typeof(ObservableCollection&lt;someClass&gt;))
                  return theObservableCollection;
              else
                  return default(T);
          }
      }
  
      //Create a public GetEnumerator method, the basic ingredient of an IEnumerable interface.
      public IEnumerator GetEnumerator()
      {
          IEnumerator r = (IEnumerator)new SettingEnumerator(this);
          return r;
      }
  
      //Create a nested-class
      class SettingEnumerator
      {
          int index;
          CustomClass&lt;T&gt; sp;
  
          public SettingEnumerator(CustomClass&lt;T&gt; str_obj)
          {
              index = -1;
              sp = str_obj;
          }
  
          public object Current
          {
              get
              {
                  return sp.theObservableCollection[index];
              }
          }
  
          public bool MoveNext()
          {
              if (index &lt; sp.theObservableCollection.Length - 1)
              {
                  index++;
                  return true;
              }
              return false;
          }
  
          public void Reset()
          {
              index = -1;
          }
      }  
  
  
  #endregion
  </code></pre>
  
  <p>The compiler complains:</p>
  
  <blockquote>
    <p>Cannot apply indexing with [] to an expression of type 'T' </p>
  </blockquote>
  
  <p>I understand that there is something wrong there, but I don't know how to accomplish what I want, which finally is to successfully make </p>
  
  <pre><code>public class CustomClass&lt;T&gt; 
  </code></pre>
  
  <p>a </p>
  
  <pre><code>public class CustomClass&lt;T&gt; : IEnumerable
  </code></pre>