I noticed that static fields of value type are stored on the heap without boxing.
For instance/non-static value type fields, I know that they are treated as parts of the object (containing the fields) on the heap so that the reference of that object is copied to the stack frame during relevant method call.
But since static value type fields do not associate with specific instance of the class, may I ask how static value type fields on the heap are accessed and modified in stack frame?
For a simple example:
class TrialsOnB
{
public static int _b = 0;
public int AccessB()
{
return _b;
}
public void ModifyB(int x)
{
_b = x;
}
}
From the accepted answer under the duplicate link, my current understanding is that in C# and Java all (kinds of) static fields are stored inside the Type
object of the class. So if a stack frame ever exists and if the data in it ever refer to static field, it may copy the reference of the Type
object and do manipulations.
If my understanding is incorrect, could anyone please correct me? Many thanks