When calling an object method on an integer literal (such as ToString), is the CLR boxing the literal first?
I wonder if boxing is taking place in order for ToString() to be called on the integer literal (5):
5.ToS开发者_JAVA百科tring();
Oh, and if not, what is taking place in order for the CLR to be able to call the ToString() method?
No, that doesn't require boxing - because int
overrides ToString
. The compiler can determine exactly which method will be called, so it doesn't need to go through virtual dispatch. It doesn't even use callvirt - that call will correspond to IL of
call instance string [mscorlib]System.Int32::ToString()
If you don't override ToString()
(etc) in a struct, then calls to the virtual method will require boxing.
精彩评论