I would like to be able to do something like this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public interface IFoo
{
IEnumerable<int> integers { get; set; }
}
public class Bar : IFoo
{
public List<int> integers { get; set; }
}
}
Why does the compiler complains..?
Error 2 'Test.Bar' does not implement interface member 'Test.IFoo.integers'. 'Test.Bar.integers' cannot implement 'Test.IFoo.integers' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable<int>'.
I understand that the interface says IEnumerable and the class uses a List, but a List is an IEnumerable.....
what can I do? I do not want to specify IEnumerable in the class, I want to use a concrete type that implements IEnumerable, like List...
Although List implements IEnumerable that's not the way interfaces work. The interface specifies exactly which types need to be exposed for properties. If you created a generic interface like
You could then use
IFoo<List<int>>
to implement it in the way you expect.You're not going to be able to use a concrete type unless you do it behind the scenes. The problem is that you can both Get and Set the Property.
Your interface specifies that the property is of type
IEnumerable<int>
.HashSet<int>
implementsIEnumerable<int>
. That means the following should work just fine:But since you're trying to implement the interface using the concrete type
List<int>
, there's no way that assignment can work.The easiest fix, assuming you don't constantly need to re-assign the collection, would be to only specify a getter for the collection:
This is a Type Covariance/Contravariance issue (see http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#C.23 ).
There's a workaround: use explicit interfaces, like so:
Note that
integers
should be TitleCased to conform to .NET's guidelines.Hopefully you can see the problem in the code above:
IList<int>
is compatible withIEnumerable<int>
only for the accessor, but not for setting. What happens if someone callsIFoo.integers = new Qux<int>()
(whereQux : IEnumerable<int>
but notQux : IList<int>
).