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:

  1. Does the compiler create a concrete, non-generic class GenericClass`string because it knows I instantiate one somewhere?
  2. If not, does the runtime compile the GenericClass<string> into a "real" class when the process starts running?
  3. If not, at what point in time does GenericClass<string> become something that a dumb CPU can understand?
  4. If this happens at runtime, does the .NET runtime somehow "cache" the work it does to resolve this?
  5. 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).

0

There are 0 answers