views:

368

answers:

1

Hi all,

Could you please clarify for me the question asked here.

Why it is important that originally defined class:

public class Metadata<DataType> where DataType : struct 
{ 
   private DataType mDataType; 
}
  1. is replaced with one derived from the same interface or abstract class is it maybe because IList<> members must share some common object type?
  2. must that common type to have the same name as IList<> type, eg. IList -> derived from T, or IList -> derived from InterfaceT, IT?

Thanks & Regards, Milan.

+4  A: 

Each generic type instantiation is a new type. i.e MetaData<int> is a different type than MetaData<bool>. The compiler generates a type like this (inspect using .Net refelector)

Namespace.Metadata`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

So you cannot declare a List of heterogeneous types. You can only declare a List of one type. Hence it is necessary to make all generic MetaData<> classes to be inherited from an abstract class or interface.

Pratik