C# .NET 4.0 and Generics

Posted by Mr Snuffle on Stack Overflow See other posts from Stack Overflow or by Mr Snuffle
Published on 2010-04-10T01:53:49Z Indexed on 2010/04/10 2:03 UTC
Read the original article Hit count: 412

I was wondering if anyone could tell me if this kind of behaviour is possible in C# 4.0

I have an object hierarchy I'd like to keep strongly typed. Something like this

class ItemBase {}

class ItemType<T> where T : ItemBase 
{
   T Base { get; set; }
}


class EquipmentBase : ItemBase {}
class EquipmentType : ItemType<EquipmentBase> {}

What I want to be able to do to have something like this

ItemType item = new EquipmentType();

And I want item.Base to return type ItemBase. Basically I want to know if it's smart enough to strongly typed generic to a base class without the strong typing. Benefit of this being I can simply cast an ItemType back to an EquipmentType and get all the strongly typedness again.

I may be thinking about this all wrong...

© Stack Overflow or respective owner

Related posts about c#

Related posts about .net-4.0