1

I have two classes

class Base
{

}
class Derived : Base
{

}

Base base = new Derived(); no compilation error

if I do ICollection<Base> collBase = new List<Derived>(); it gives the compilation error. Is there any other alternatives to solve this?

Praveen
  • 304
  • 2
  • 11
  • [some reading](http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx) about covariance and contra variance will be helpful. – Sam Holder Jun 08 '11 at 12:55
  • which framework are you working with? if you are working with <4, you may have a look at http://stackoverflow.com/questions/833447/why-is-this-cast-not-possible –  Jun 08 '11 at 12:55
  • What would you want to happen if someone then did `collBase.Add(new Derived2())`, where `Derived2` is another class that derives from `Base`? If you only want to be able to *read* from `collBase`, you can do what @asawyer suggests. If not, you need to think some more. – AakashM Jun 08 '11 at 12:56
  • 2
    @AakashM: as long as he uses .net 4.0 ... if he's running on a lower version, the answer of @asawyer won't work! –  Jun 08 '11 at 13:03
  • That is true, I'll edit to reflect. – asawyer Jun 08 '11 at 13:08
  • @Andreas good point, I forget so easily... – AakashM Jun 08 '11 at 13:08

1 Answers1

4

If you are using .Net version 4 : Change ICollection to IEnumerable

http://msdn.microsoft.com/en-us/library/dd799517.aspx

Edit - more useful reading

http://blogs.msdn.com/b/ericlippert/archive/2007/10/26/covariance-and-contravariance-in-c-part-five-interface-variance.aspx

http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/default.aspx

attempting to clarify further why you can't do this:

class animal {}
class dog : animal {}
class cat : animal {}

ICollection<animal> dogs = new List<dog>(); //error (Or List<T>, same error) because...
dogs.Add(new cat()); //you just dogged a cat

IEnumerable<animal> dogs = new List<dog>(); // this is ok!
asawyer
  • 17,642
  • 8
  • 59
  • 87
  • c'mon ... `System.Collection.Generic.List` implements `System.Collection.ICollection` - that is not the problem! ... i'm considering a downvote - you should elaborate the meaning of covariance and contravariance - not only throwing some links in! –  Jun 08 '11 at 12:57
  • @Andreas ICollection is not covariant safe, IEnumerable is because you cannot Add to the set. Take a look at some of Eric Lipperts writing on the subject, he explains things better then I ever could. – asawyer Jun 08 '11 at 13:02
  • @asawyer: another fact which matters: .net version ... :) btw ... thanks for elaborating - much clearer now! –  Jun 08 '11 at 13:05
  • @asawyer `IEnumerable dogs = new List(); // this is ok!` also giving the error :( – Praveen Jun 08 '11 at 13:32
  • @praveen: please elaborate: which .net version are you using! –  Jun 08 '11 at 13:34
  • @praveen Andreas is correct, this is a feature added in .Net version 4. If your compiling on an earlier version @Andreas posted a comment to your question with a link to another workaround. – asawyer Jun 08 '11 at 13:38