Where is it more appropriate to use
class Entity<T> {}
new Entity<User>()
as opposed to
class Entity
{
public Entity(System.Type type)
{
}
}
new Entity(typeof(User))
I realized the significance of System.Type after dealing with reflection and code generation. After a month of development and getting familiar, I look back at my choices with skepticism.
This would probably not apply to you if you do not need reflection.
The generic approach
Entity<T>has the ability to use generic type constraints to enforce both rules and features at compile-time (i.e.Tmust have a public parameterless constructor and be a reference-type, for example). It doesn’t need any storage space for the type on each instance, sinceEntity<X>andEntity<Y>are discreet types – but as a re-statement of that: you cannot have a heterogeneousList<Entity<?>>(unless you have a common base-type) for the same reason.It is much harder to use generics if the type is known only at run-time.