简体   繁体   English

在C#中的此Boxing示例中会发生什么?

[英]What happens in this Boxing example in C#?

Jon Skeet, has an interesting post titled: " Why boxing doesn't keep me awake at night " where he benchmarks the performance of different ways of outputting an integer value. 乔恩·斯凯特(Jon Skeet)有一篇有趣的帖子,标题为:“ 为什么拳击不能让我彻夜难眠 ”,他在此基准测试了输出整数值的不同方式的性能。

I am pretty sure the code below IS boxing, but why Jon is considering it NOT to be boxing? 我很确定下面的代码拳击,但乔恩(Jon)为什么认为它不是拳击? his example is at the end. 他的榜样在最后。

int i = 5;
object o = i;
Console.WriteLine("Number is: {0}", o);

The example from Jon's page: Jon页面上的示例:

#if CONSOLE_WITH_BOXING
            Console.WriteLine("{0} {1} {2}", i, i, i);            
#elif CONSOLE_NO_BOXING
            object o = i;
            Console.WriteLine("{0} {1} {2}", o, o, o);
#elif CONSOLE_STRINGS
            string s = i.ToString();
            Console.WriteLine("{0} {1} {2}", s, s, s);

PS "boxing and unboxing in int and string" does not answer my question. PS “装箱和拆箱int和字符串”不能回答我的问题。

Thank you. 谢谢。

It is boxing, the only difference is on what line it is happening: 这是拳击,唯一的不同是发生在哪一行:

not boxing (see http://msdn.microsoft.com/en-us/library/a0bfz20d%28v=vs.110%29.aspx ): 不装箱(请参阅http://msdn.microsoft.com/zh-cn/library/a0bfz20d%28v=vs.110%29.aspx ):

Console.WriteLine("{0} {1} {2}", o, o, o);

boxing: 拳击:

object o = i;

or consider 或考虑

three boxing(s): 三个拳击:

Console.WriteLine("{0} {1} {2}", i, i, i);

one boxing: 一拳:

object o = i;
Console.WriteLine("{0} {1} {2}", o, o, o);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM