开发者

When calling an object method on an integer literal (such as ToString), is the CLR boxing the literal first?

开发者 https://www.devze.com 2023-02-11 15:14 出处:网络
I wonder if boxing is taking place in order for ToString() to be called on the integer literal (5): 5.ToS开发者_JAVA百科tring();

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消