Sorry if this has been asked before but it’s virtually impossible to google. I think that an int array implements IEnumerable and therefore Thing should be able to implement IThing. How come it doesn’t?
public interface IThing
{
IEnumerable<int> Collection { get; }
}
public class Thing : IThing
{
public int[] Collection { get; set; }
}
note that
public class Thing : IThing
{
public int[] Array { get; set; }
public IEnumerable<int> Collection
{
get
{
return this.Array;
}
}
}
is fine.
The interface implementation must implement the interface exactly. This prevents you from returning a type that implements that interface as the member.
If you wish to do this, one option is to implement the interface explicitly:
This allows your public API for the class to use the concrete type, but the interface implementation to be fulfilled correctly.
For example, with the above, you can write: