C # - Multiple Common Types in One List II

Could you clarify for me the question asked here .

Why it is important that the originally defined class:

public class Metadata<DataType> where DataType : struct { private DataType mDataType; } 
  • is replaced by one obtained from the same interface or abstract class, perhaps because IList <> members must have a common object type?
  • must have a common type with the same name as the IList <> type, for example. IList β†’ derived from T, or IList β†’ derived from InterfaceT, IT?

Thanks and respect, Milan.

+4
source share
1 answer

Each instance of a type type is a new type. i MetaData<int> is a different type than MetaData<bool> . The compiler generates this type (check the use of the .Net reflector)

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

Thus, you cannot declare a list of heterogeneous types. You can only declare a list of one type. Therefore, it is necessary that all generic MetaData <> classes inherit from an abstract class or interface.

+8
source

All Articles