I'm sure the answer is out there in ten forms already, but unfortunately, I don't know how else to form the question.
If I make a class with a generic Type
parameter:
public class GenericClass<TTypeParam> where TTypeParam : class
{
public TTypeParam InstanceOfTypeTTypeParam { get; set; }
public override string ToString()
{
return (this.InstanceOfTypeTTypeParam == null) ? string.Empty : this.InstanceOfTypeTTypeParam.ToString();
}
}
And then use it somewhere:
public static void Main()
{
var instance = new GenericClass<string>();
System.Diagnostics.Debug.WriteLine(instance.ToString());
}
For the purpose of better understanding the mechanisms behind the .NET runtime, I am interested to know:
- Does the compiler create a concrete, non-generic class
GenericClass`string
because it knows I instantiate one somewhere? - If not, does the runtime compile the
GenericClass<string>
into a "real" class when the process starts running? - If not, at what point in time does
GenericClass<string>
become something that a dumb CPU can understand? - If this happens at runtime, does the .NET runtime somehow "cache" the work it does to resolve this?
- Is this a behavior that is configurable on a per-class or per-assembly or some other basis? Or is it not configurable? (IE could you ask it to do this work up-front if the default behavior is to do it when it needs to, or vice-versa?)
If it happens right before each call, I would think that this could incur some overhead and also that maybe it would be tough sometimes to predict the difficulty upfront of translating some high-level .NET
instructions into real instructions.
I am also wondering as a follow-up whether if the compiler does a static analysis of all the permutations of the uses of generic types (if 1 is true), then if it also does the same thing with generic methods up front.
If you have the time I would appreciate if you could clue me into the correct terminology for these concepts (where ever I am lacking).