Question

The following code allows me to store a value for each type T:

public static class MyDict<T> {
    public static T Value;
}

I can store as many values as there are types and the compiler doesn't know before-head what types I'm going to use. How and where are those static field values stored?

Update: Obviously it's stored in memory, but I want to know about this memory. Is it heap? Is it some special CLR memory? How is it called? What else is stored that way?

Update 2: JITter generates a single implementation MyDict<__Canon> for all reference type arguments of MyDict<T>. Yet, the values are stored separately. I guess that there is still some per-type-argument structure for each type argument, and while thw vtable is linked to the JITted MyDict<__Canon>, the fields are separate. Am I right?

Was it helpful?

Solution

How and where are those static field values stored?

They are stored in memory at a location of the CLR's choosing.

Obviously it's stored in memory, but I want to know about this memory.

I assume out of curiosity. If you are making a programming decision based on the answer to this question then you are doing something wrong.

Is it heap?

Well it's not stack or registers, that's for sure.

Is it some special CLR memory?

Yep.

What is it called?

The high frequency heap.

What else is stored that way?

vtables. Interface map structures. Method descriptions. And anything else that the CLR believes is going to be accessed frequently, at the sole discretion of the CLR. We are deep in implementation details here.

JITter generates a single implementation MyDict<__Canon> for all reference type arguments of MyDict<T>.

Correct, though this is an implementation detail.

Yet, the values are stored separately.

By "the values" you mean "the values of static fields of each constructed type". Yes.

I guess that there is still some per-type-argument structure for each type argument

Yes, the data has to go somewhere!

the vtable is linked to the JITted MyDict<__Canon>, the fields are separate.

I don't understand what this sentence means, so I am unable to confirm or deny its correctness.

I also wonder whether there is a way to have per-object stores like that. I.e. not generic type + T, but object + T

To clarify, your question is: there is some storage mechanism that associates a generic type C<T> and a given construction C<Foo> with a given static field of C<Foo>. We can think of this as a lookup where the "key" is the tuple (C<T>, Foo, field) and the value is the value of the field. Is there a similar storage mechanism where the key is (C<T>, some arbitrary object, field)?

No. Build it yourself if you need it.

OTHER TIPS

MyDict<T> is not a fully defined type. Each fully defined type of MyDict<T> will have its own unique instance of Value (e.g. MyDict<string>, MyDict<object>, and MyDict<int> can have a unique value of Value).

Also, this isn't special because the type of Value is T, even if the type of Value were DateTime, each fully defined type would still have its own instance of the static value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top