c# - "Covariance" in template parameters -
let's have these 2 classes class baseclass { protected hashset<baseclass> container; } class derivedclass : baseclass { derivedclass() { container = new hashset<derivedclass>(); } } then receive error: unable convert. since every derivedclass (should) baseclass, i'm not quite sure why error being thrown, yet is. the goal baseclass perform variety of operations on container , particularly specific behaviors tied derivedclass - among those, requiring container of type hashset<derivedclass> . how goal accomplished? every devrivedclass baseclass , not other way around. hashset<t> cannot covariant since allows write operations ( add ). in scenario possible: class baseclass { protected hashset<baseclass> container; public dosomething() { container.add(new baseclass()); // not legal if container list<derivedclass> } } you change type of contain...