"Interface not implemented" when Returning Derived Type
<p>The following code:</p>

<pre><code>public interface ISomeData
{
    IEnumerable&lt;string&gt; Data { get; }
}

public class MyData : ISomeData
{
    private List&lt;string&gt; m_MyData = new List&lt;string&gt;();
    public List&lt;string&gt; Data { get { return m_MyData; } }
}
</code></pre>

<p>Produces the following error:</p>

<blockquote>
  <p>error CS0738: 'InheritanceTest.MyData'
  does not implement interface member
  'InheritanceTest.ISomeData.Data'.
  'InheritanceTest.MyData.Data' cannot
  implement
  'InheritanceTest.ISomeData.Data'
  because it does not have the matching
  return type of
  'System.Collections.Generic.IEnumerable'.</p>
</blockquote>

<p>Since a List&lt;T&gt; implements IEnumerable&lt;T&gt;, one would think that my class would implement the interface. Can someone explain what the rationale is for this not compiling?</p>

<p>As I can see it, there are two possible solutions:</p>

<ol>
<li>Change the interface to be more specific and require IList be implemented.</li>
<li>Change my class (MyData) to return IEnumerable and implement the original interface.</li>
</ol>

<p>Now suppose I also have the following code:</p>

<pre><code>public class ConsumerA
{
    static void IterateOverCollection(ISomeData data)
    {
        foreach (string prop in data.MyData)
        {
            /*do stuff*/
        }
    }
}

public class ConsumerB
{
    static void RandomAccess(MyData data)
    {

        data.Data[1] = "this line is invalid if MyPropList return an IEnumerable&lt;string&gt;";
    }
}
</code></pre>

<p>I could change my interface to require IList to be implemented (option 1), but that limits who can implement the interface and the number of classes that can be passed into ConsumerA. Or, I could change implementation (class MyData) so that it returns an IEnumerable instead of a List (option 2), but then ConsumerB would have to be rewritten.</p>

<p>This seems to be a shortcoming of C# unless someone can enlighten me.</p>
