Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Multiple generic types in one list II

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.

like image 893
Milan Avatar asked Feb 28 '23 08:02

Milan


1 Answers

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.

like image 160
softveda Avatar answered Mar 12 '23 19:03

softveda