I have a structure like below. I have some trouble returning a generic collection. What am I missing ?
class Program
{
static void Main()
{
BusinessCollection businessCollection = new BusinessCollection();
//Why this is not working because businesscollection is a GenericCollection<BusinessEntity>
businessCollection = new GenericCollection<BusinessEntity>();
//or neither this
businessCollection = (BusinessCollection)new GenericCollection<BusinessEntity>();
}
}
public class BusinessEntity
{
public string Foo { get; set;}
}
public class BusinessCollection : GenericCollection<BusinessEntity>
{
//some implementation here
}
public class GenericCollection<T> : ICollection<T>
{
//some implementation here
}
You can’t do what you want to do. The other way around will work.
All
BusinessCollectionare indeedGenericCollection<BusinessEntity>but you can’t say for sure that allGenericCollection<BusinessEntity>areBusinessCollection‘sSo the following will work.