0

To simplify my question, I'm using IList and IList<T> as the example. Since IList declared a method Add(object value) and IList<T> declared a method Add(T value). My new class has to have two methods for implementation.

class myList<T> : IList<T>, IList
{
  public void IList.Add(object value)
  {
    this.Add(value as T);
  }
  public void Add(T value)
  {...}
}

Is it possible to avoid such "meaningless" replication?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Karl Jr.
  • 15
  • 4
  • If you're implementing an interface (or a set of interfaces) that dictate that you need both, then you need to declare both. Period. You can usually implement one of them and let all the others call the first one though. – Lasse V. Karlsen Jun 29 '17 at 09:03
  • You're implementing both `IList` and `IList` so expect the duplications. They're not the same interface - one handles generic implementations the other standard objects. You get the same with most IEnumerable derived interfaces, I think this is due to the enumerators but can remember off the top of my head. – Callum Linington Jun 29 '17 at 09:06
  • 1
    As long as you try to duplicate 15 year old mistakes, no. The code is wrong, it must use this.Add((T)value). – Hans Passant Jun 29 '17 at 09:08
  • If you're looking for something that reduces replication look at inheritance not interfaces. An interface is simply a contract that a class must follow in order for it to be used in a certain way. – Ryan Searle Jun 29 '17 at 09:17
  • Thanks forks. I have to use the non-generic type interface due to the object types also implement multiple interfaces and need to be a class without a base class to call, and they are determined in runtime. it's a very special case. – Karl Jr. Jun 29 '17 at 09:53

3 Answers3

0

The IList collection stores object instances, but IList<T> stores specific type instances. The Add methods has different signatures for IList and IList<T> interfaces. Therefore you need to declare both methods.

Alexander
  • 4,420
  • 7
  • 27
  • 42
0

No you can't avoid this. Because this would violate the principle of interfaces. Think about a client program which just works with IList. This program want to use your class now, which works of course, because you implement IList. So he calls the Method:

public void IList.Add(object value)
{
  this.Add(value as T);
}

What should happen now, if you wouldn't implement this? Sure you can call Add<T> instead, but if there are many classes which implement IList and you call Add-Method on an IList interface to use polymorphism under the hood this would not work anymore.

C# is strict here for good reason, same as any other language with Interface Concept I know.

Sebi
  • 3,879
  • 2
  • 35
  • 62
0

Maybe you should avoid implementing non generic IList because it violates SOLID and more precisely Liskov substitution principle. It's a legacy interface from .Net 1 when generics where not available. Well if you are dealing with legacy code, it can be necessary.

If you have to implement IList, has stated by Hans, use an implementation that will fail fast : throw an exception when the provided object is not allowed in your list.

void IList.Add(object value)
{
  this.Add((T)value);
}
Guillaume
  • 12,824
  • 3
  • 40
  • 48