Igal Yoffe <[EMAIL PROTECTED]> wrote:

> Consider the following code:
>   Console.Writeline("{0} {2}",1,(new MyStruct()).ToString());
> 
> The number of boxings in such code is between 0 to 2, depending on the
> implementation of the compiler.
> 
> Straightforwardly, two boxings are obvious, viz., int boxing and struct's
> boxing before calling ToString. However, it could be the case that in
> compile time the compiler creates "object(1)" (really a trivial
> optimization) for the integer,

The boxed struct needs to live on the heap, so it can outlive the call,
in case Console.WriteLine keeps a reference to the passed-in value.
Therefore, it (the C# compiler) can't remove the box, and must logically
allocate heap memory.

With deeper analysis after run-time linking, a JIT could possibly think
about doing something - but the box instruction is a CIL primitive, so
the logical box would still exist.

-- Barry

-- 
http://barrkel.blogspot.com/

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to