T
Tom
Couple of questions relating to boxing. Firstly, I already know that boxing
is the processing of temporarily copying a ValueType (e.g. struct, enum) to
the heap so that the system can treat a value type like a reference type.
However, I have some questions relating to implicit boxing:
1. If I add custom instance method on a struct, will it box that type each
time the method is called? For example, suppose I have the following:
public struct MyStruct
{
private string _value;
public string Value
{
get {return _value;}
set {_value = value;}
}
public void DoInstanceStuff()
{
}
public static void DoStaticStuff()
{
}
}
MyStruct var;
var.DoInstanceStuff(); //<-- does this line box var?
2. Same question as #1 only relating to DoStaticStuff given that it is a
static method.
3. How do I substantiate the answer to either of the above questions?
4. If a value type is boxed, unboxed and then boxed again, does the system
reuse the original boxing's heap allocation or re-allocate a new area in the
heap. I'm presuming the later, but I wondered if the framework included
some sort of boxing optimization.
5. If I box a value type (explicit or implicit) and, while boxed, change one
of the value type's underlying field values (like through one of the value
type's methods), what does the system do?
Thanks,
Tom
is the processing of temporarily copying a ValueType (e.g. struct, enum) to
the heap so that the system can treat a value type like a reference type.
However, I have some questions relating to implicit boxing:
1. If I add custom instance method on a struct, will it box that type each
time the method is called? For example, suppose I have the following:
public struct MyStruct
{
private string _value;
public string Value
{
get {return _value;}
set {_value = value;}
}
public void DoInstanceStuff()
{
}
public static void DoStaticStuff()
{
}
}
MyStruct var;
var.DoInstanceStuff(); //<-- does this line box var?
2. Same question as #1 only relating to DoStaticStuff given that it is a
static method.
3. How do I substantiate the answer to either of the above questions?
4. If a value type is boxed, unboxed and then boxed again, does the system
reuse the original boxing's heap allocation or re-allocate a new area in the
heap. I'm presuming the later, but I wondered if the framework included
some sort of boxing optimization.
5. If I box a value type (explicit or implicit) and, while boxed, change one
of the value type's underlying field values (like through one of the value
type's methods), what does the system do?
Thanks,
Tom