This will always cause one box operation. The version of WriteLine that will
be called will be the one with the signature

WriteLine(string format, params object[] args);

Therefore all the args will be treated as object - this means the 1 will be
boxed. However, the second argument due to operator precedence will create
the struct, and call its ToString method. The compiler knows exactly which
ToString method must be called - if MyStruct overrides ToString then it will
be the MyStruct version if not it will be System.Object's version -
therefore, no virtual dispatch is required and therefore no boxing.

Regards

Richard Blewett - DevelopMentor

> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Igal Yoffe
> Sent: 07 August 2007 08:20
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: [ADVANCED-DOTNET] compiler considerations
>
> 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, and moreover have special vtable logic
> for
> structures (recall that structures could not be inherited) which calls
> ToString directly, thus no boxings at all.
>
> While I could examine the ilasm generated by _my_ compiler I would be
> really glad to know what do you think _typical_ (e.g. microsoft's csc)
> compiler would do?
>
> ===================================
> This list is hosted by DevelopMentorR  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.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