title: Overloaded indexer with enum : impossible to use default indexer
body: <p>Considering the following code:</p>
  
  <pre><code>namespace MyApp
  {
      using System;
      using System.Collections.ObjectModel;
  
      class Program
      {
          static void Main(string[] args)
          {
              var col = new MyCollection();
              col.Add(new MyItem { Enum = MyEnum.Second });
              col.Add(new MyItem { Enum = MyEnum.First });
  
              var item = col[0];
              Console.WriteLine("1) Null ? {0}", item == null);
  
              item = col[MyEnum.Second];
              Console.WriteLine("2) Null ? {0}", item == null);
  
              Console.ReadKey();
          }
      }
  
      class MyItem { public MyEnum Enum { get; set; } }
  
      class MyCollection : Collection&lt;MyItem&gt;
      {
          public MyItem this[MyEnum val]
          {
              get
              {
                  foreach (var item in this) { if (item.Enum == val) return item; }
                  return null;
              }
          }
      }
  
      enum MyEnum
      {
          Default = 0,
          First,
          Second
      }
  }
  </code></pre>
  
  <p>I was surprised to see the following result:</p>
  
  <pre><code>1) Null ? True
  2) Null ? False
  </code></pre>
  
  <p>My first expectation was that because I was passing an <code>int</code>, the default indexer should be used, and the first call should have succeeded.</p>
  
  <p>Instead, it seems that the overload expecting an <code>enum</code> is always called (even when casting 0 as int), and the test fails.</p>
  
  <ol>
  <li>Can someone explain this behavior to me?</li>
  <li>And give a workaround to maintain two indexers: one by index, and one for the enum?</li>
  </ol>
  
  <p><strong>EDIT</strong> : A workaround seems to be casting the collection as Collection, see <a href="https://stackoverflow.com/questions/7474609/overloaded-indexer-with-enum-impossible-to-use-default-indexer/7475081#7475081">this answer</a>.</p>
  
  <p>So:</p>
  
  <ol start="3">
  <li>Why does the compiler choose the most "complex" overload instead of the most obvious one (despite the fact it's an inherited one)? Is the indexer considered a native int method? (but without a warning on the fact that you hide the parent indexer)</li>
  </ol>
  
  <p><strong>Explanation</strong></p>
  
  <p>With this code we are facing two problems:</p>
  
  <ol>
  <li>The 0 value is always convertible to any enum.</li>
  <li>The runtime always starts by checking the bottom class before digging in inheritance, so the enum indexer is chosen.</li>
  </ol>
  
  <p>For more precise (and better formulated) answers, see the following links:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7474609/overloaded-indexer-with-enum-impossible-to-use-default-indexer/7475081#7475081">original answer</a> by James Michael Hare</li>
  <li><a href="https://stackoverflow.com/questions/7474609/overloaded-indexer-with-enum-impossible-to-use-default-indexer/7477463#7477463">sum up</a> by Eric Lippert</li>
  </ul>