IEnumerable<string> in interface. But List<string> in implementation class. RRS feed

  • Question

  • an interface with a getter property of IEnumerable<string> cannot be implemented by a class with a property with List<string>. Why not?  

    Seems like it would be a neat feature.  One class implements the IEnumerable as a List. Another as ObservableCollection.  But both classes support reading from the IEnumerable thru the interface.

      public interface IDemo
        IEnumerable<string> Names { get; }
      public class DemoClass : IDemo
        public List<string> Names
        { get; set; }
    Error	CS0738	'DemoClass' does not implement interface member 'IDemo.Names'. 
    'DemoClass.Names' cannot implement 'IDemo.Names' because it does not have the matching return type of 'IEnumerable<string>'

    Saturday, May 28, 2016 5:11 PM