0

It's highly likely this question will reveal a misunderstanding on my part of how to create good interfaces...

Assume I have a class ItemClass and it's implementing the IItemInterface interface, like so:

public class ItemClass : IItemInterface /* ..... */ 

I also have the following interface:

public interface ClassInterface
{
    List<IItemInterface> Items;
}

Now, lets say in a specific implementation of this interface, I want to my Items property to be a more specific than a simple list of IItemInterface. C# allows me to override the ClassInterface Items property and do this (the code compiles/runs perfectly):

public class MyClass : ClassInterface
{
    public List<ItemClass> Items { get; set;  }

    IList<IItemInterface> MyInterface.Items
    {
        get { return this.Items; }
    }
}

// Method somewhere else
public void foo()
{
    MyClass bar = new MyClass();
    bar.Items = new List<ItemClass>();
    // etc....
}

My question is: why does C# allow me to do this? I know you are not allowed to overload properties, so why is an interface treated differently?

JarbingleMan
  • 350
  • 1
  • 12

0 Answers0