Hi Jon,
Although i am sure that internally no boxing takes place in this case,
but surprisingly the compiler does generate the box instruction.
I compiles this code
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Test t = new Test();
ArrayList al = new ArrayList();
al.Add(t.a);
}
}
public class Test
{
public int a = 5;
}
and i got this IL
..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() =
( 01 00 00 00 )
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] class ConsoleApplication1.Test t,
[1] class [mscorlib]System.Collections.ArrayList al)
IL_0000: newobj instance void ConsoleApplication1.Test::.ctor()
IL_0005: stloc.0
IL_0006: newobj instance void
[mscorlib]System.Collections.ArrayList::.ctor()
IL_000b: stloc.1
IL_000c: ldloc.1
IL_000d: ldloc.0
IL_000e: ldfld int32 ConsoleApplication1.Test::a
IL_0013: box [mscorlib]System.Int32 // Box call is generated
IL_0018: callvirt instance int32
[mscorlib]System.Collections.ArrayList::Add(object)
IL_001d: pop
IL_001e: ret
} // end of method Class1::Main
Sijin Joseph
http://www.indiangeek.net http://weblogs.asp.net/sjoseph
Jon Skeet [C# MVP] wrote:
Sijin Joseph <si***************@hotmail.com> wrote:
That's a great article Jon! I sure did miss the part where Value types
are fields of Reference types. But one question comes to mind, in this
case will boxing occur? (since the value is already on the heap)
No. Boxing occurs when you need to view a value type as a reference
type - it's sort of when a value type value needs to be on the heap "on
its own" rather than as part of another object.