<p>I've got a collection that implements an interface that extends both IList&lt;T> and List. </p>

<pre><code>public Interface IMySpecialCollection : IList&lt;MyObject&gt;, IList { ... }
</code></pre>

<p>That means I have two versions of the indexer.  </p>

<p>I wish the generic implementation to be used, so I implement that one normally:</p>

<pre><code>public MyObject this[int index] { .... }
</code></pre>

<p>I only need the IList version for serialization, so I implement it explicitly, to keep it hidden:</p>

<pre><code>object IList.this[int index] { ... }
</code></pre>

<p>However, in my unit tests, the following</p>

<pre><code>MyObject foo = target[0];
</code></pre>

<p>results in a compiler error</p>

<blockquote>
  <p>The call is ambiguous between the
  following methods or properties</p>
</blockquote>

<p>I'm a bit surprised at this; I believe I've done it before and it works fine.  What am I missing here?  How can I get IList&lt;T> and IList to coexist within the same interface?</p>

<p><strong>Edit</strong> IList&lt;T> does <em>not</em> implement IList, and I <strong>must</strong> implement IList for serialization.  I'm not interested in workarounds, I want to know what I'm missing.</p>

<p><strong>Edit again</strong>:  I've had to drop IList from the interface and move it on my class.  I don't want to do this, as classes that implement the interface are eventually going to be serialized to Xaml, which requires collections to implement IDictionary or IList...</p>

[{'answer_id': 57072, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': -1, 'selected': False, 'text': "<p>List&lt;T> implies IList, so it's a bad idea to use both in the same class.</p>\n"}, {'answer_id': 57084, 'author': 'Adrian Clark', 'author_id': 148, 'author_profile': 'https://Stackoverflow.com/users/148', 'pm_score': 1, 'selected': False, 'text': '<p>Unfortunately you can\'t declare two indexers with the same parameter list.  The following paragraph is taken from here <a href="http://msdn.microsoft.com/en-us/library/2549tw02.aspx#languageReferenceRemarksToggle" rel="nofollow noreferrer" title="C# Programming Guide - Using Indexers">C# Programming Guide - Using Indexers "Remarks" section</a>:</p>\n\n<blockquote>\n  <p>The signature of an indexer consists of the number and types of its formal parameters. It does not include the indexer type or the names of the formal parameters. If you declare more than one indexer in the same class, they must have different signatures.</p>\n</blockquote>\n\n<p>You will have to declare a different set of parameters if you wish to use the second indexer.</p>\n'}, {'answer_id': 57093, 'author': 'Jarrett Meyer', 'author_id': 5834, 'author_profile': 'https://Stackoverflow.com/users/5834', 'pm_score': -1, 'selected': False, 'text': "<p>Change your generic implementation to...</p>\n\n<pre><code>T IList&lt;T&gt;.this[int index] { get; set; }\n</code></pre>\n\n<p>This explicitly says which 'this' is which.</p>\n"}, {'answer_id': 57166, 'author': 'Darryl Braaten', 'author_id': 1834, 'author_profile': 'https://Stackoverflow.com/users/1834', 'pm_score': 3, 'selected': True, 'text': "<p>You can't do this with </p>\n\n<p><code>public interface IMySpecialCollection : IList&lt;MyObject&gt;, IList { ... }</code></p>\n\n<p>But you can do what you want with a class, you will need to make the implementations for one of the interfaces explicit. In my example I made IList explicit.</p>\n\n<p><code>public class MySpecialCollection : IList&lt;MyObject&gt;, IList { ... }</code></p>\n\n<p><code>IList&lt;object&gt; myspecialcollection = new MySpecialCollection();\n IList list = (IList)myspecialcollection;</code></p>\n\n<p>Have you considered having IMySpecialCollection implement ISerializable for serialization?\nSupporting multiple collection types seems a bit wrong to me. You may also want to look at casting yout IList to IEnumerable for serialization since IList just wraps IEnumerable and ICollection. </p>\n"}, {'answer_id': 1669935, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<p>This is a dupe of <a href="https://stackoverflow.com/questions/1552456/icollection-vs-icollectiont-ambiguity-between-icollectiont-count-and-icollec">my question here</a></p>\n\n<p>To summarise, if you do this, it solves the problem:</p>\n\n<pre><code>public Interface IMySpecialCollection : IList&lt;MyObject&gt;, IList\n{\n    new MyObject this[int index];\n    ... \n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/57054', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']