Suggestion: compiler warning/info for implicit boxing

J

J.Marsch

I don't know whether this is the appropriate place to give product feedback,
but here goes:

I would love to see some kind of diagnostic to let me know when implicit
boxing has occurred.

We have had a few instances where one developer or another was working on
code, and did not realize that by passing their value type to a method that
accepted an object parameter (or in many cases, a delegate).

That's not an extremely common occurrence, but from time to time it can
happen, and in a team environment, it can be introduced by any developer,
and once it's done, it's not extremely noticeable. I would really love to
be able to do a build, or launch a probe, and be warned of lines where
implicit boxing is occurring. Then I or another team member could review
that code, to determine whether the boxing was intentional or accidental.
 
J

Jeffrey Tan[MSFT]

Hi ,

I will do some research to find if there is a way to detect implicit boxing.
I will reply to you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi ,

You can use the ILDASM tool to open the compiled assembly and check the IL
code to see if there is boxing involved. For example, the following C# code:
int i = 123;
object o = i;
will be compiled to the following IL code:
IL_0000: ldc.i4.s 123
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: box [mscorlib]System.Int32
IL_0009: stloc.1
So it is boxed.

Actually, the following documentation lists the situations when boxing will
occur.
http://msdn.microsoft.com/library/?url=/library/en-us/csspec/html/vclrfcshar
pspec_4_3_1.asp?frame=true

For suggestion to Microsoft, you can visit:
http://register.microsoft.com/mswish/suggestion.asp?&SD=GN&LN=EN-US&gssnb=1
or you can mail to (e-mail address removed)

Your suggestion will make our product more efficient, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

J.Marsch

Thank you for your help Jeffrey. In leiu of a warning from the tool,
perhaps we can make use of ILDASM by scanning the dissassembly of our
assemblies for instances of boxing.

"Jeffrey Tan[MSFT]" said:
Hi ,

You can use the ILDASM tool to open the compiled assembly and check the IL
code to see if there is boxing involved. For example, the following C# code:
int i = 123;
object o = i;
will be compiled to the following IL code:
IL_0000: ldc.i4.s 123
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: box [mscorlib]System.Int32
IL_0009: stloc.1
So it is boxed.

Actually, the following documentation lists the situations when boxing will
http://msdn.microsoft.com/library/?url=/library/en-us/csspec/html/vclrfcshar
pspec_4_3_1.asp?frame=true

For suggestion to Microsoft, you can visit:
http://register.microsoft.com/mswish/suggestion.asp?&SD=GN&LN=EN-US&gssnb=1
or you can mail to (e-mail address removed)

Your suggestion will make our product more efficient, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi,

If you want to find out all of the places in your code where a boxing
operation occurs, you can dump the assembly to an output file using ildasm
and grep for the box instruction in your IL.

Happy Thanksgiving!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top