2

Basically I have a method with the following signature:

public void Save(BindingList<T> items);

and I'm trying to call it using

classInstance.Save(items); //items = BindingList<ObjectInheritedFromT>

but it seems that C# doesn't realize the inheritance throwing a compile-time error, where all code written for T would be completely functional for ObjectInheritedFromT as well, wouldn't it?.

Is there a work around for this? I realized this works if it were IEnumerable, but then (oddly enough) it turns out BindingList<> doesn't implement IEnumerable, and I need to perform operations such as Remove and Add.

PedroC88
  • 3,708
  • 7
  • 43
  • 77

1 Answers1

3

Because BindingList<T> isn't declared as BindingList<out T> - it doesn't support covariance

Give this a read:

Covariance and Contravariance FAQ

JerKimball
  • 16,584
  • 3
  • 43
  • 55
  • I read about it a while ago and now that you mention it I remembered. Is there any way to go around this? – PedroC88 Jan 04 '13 at 20:56
  • You could `.Cast` it, or otherwise mangle it to the expected type, but no, there's no workaround that'll affect `BindingList` itself. – JerKimball Jan 04 '13 at 20:59