Re:Having a Go - first time experience with Golang
by Someone on Monday July 09, 2012 @03:20PM
(#4219896)
In C#, generic classes cannot be co- or contravariant on their types, but interfaces can. IList<T> itself is not covariant, as that would open the hole you mention, but it extends interface of IEnumerable<T>, which is covariant (it only allows you take T's out of the List, so it is OK if you do
var subs = new List<Sub>();
...
IEnumerable<Base> bases = subs;
The objects that 'subs' produces are guaranteed to be Sub's, and hence Base's, and you cannot insert a Base into 'subs' this way because IEnumerable does not have a method for doing that.
More info at http://msdn.microsoft.com/en-us/library/dd799517.aspx.