Recently asked the question, I answered yes, because. toString () Converts the value type 2 to a reference type, so boxing occurs.
It's a bit inappropriate, so check out some information and refer to the discussion on the Web:
The official explanation of unboxing:
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value Typ E. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; Unboxing is explicit. The concept of boxing and unboxing underlies the C # Unified view of the type system in which a value of any type can be TR Eated as an object.
boxing is used to store value types in managed memory. Boxing is an implicit conversion of a value type to an object type or to any interface type implemented by this value type. Boxing A value type allocates an object instance in the heap and copies the value to the new object.
Here are some examples of the official unboxing/crating:
eg1:int i = 123;object o = i;// The implicit boxing eg2:String.Concat ("Answer", 42, true) &NBSP;//42 and true will occur boxing eg3:list<object> mixedlist = new List<object> (); Mixedlist.add ("First group:");for (int j = 1; j < 5; j++) { mixedlist.add (j);//When added, J Boxing first}var sum = 0;for (var j = 1; j < 5; j++) { A compilation error: //operator ' * ' occurs on the following line cannot be applied to operands of type ' object ' and ' object ' . //sum += MIXEDLIST[J]&NBSP;*&NBSP;MIXEDLIST[J]) //The above compilation error will not occur after unpacking. sum += (int) mixedlist[j] * (int) mixedlist[j];}
Note:
The boxing and unboxing processes require a lot of computation relative to the simple assignment. when you are boxing a value type, you must assign and construct a new object. the mandatory conversions required for unboxing also require a lot of computation, just a lesser degree.
More about performance: https://msdn.microsoft.com/zh-cn/library/ms173196.aspx
Then introduce the picture to illustrate the changes in memory:
int i = 123;object o = i;//implicit boxing
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/EA/wKioL1dk8QeAuJr0AAAIh9hVWI8664.png "style=" float: none; "title=" 2016-06-18_145730.png "alt=" Wkiol1dk8qeaujr0aaaih9hvwi8664.png "/>
int i = 123; A value typeobject o = i; Boxingint j = (int) o; Unboxing
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/EA/wKioL1dk8QeSbf--AAAJ2r47a3g046.png "title=" 2016-06-18_145749.png "style=" Float:none; "alt=" Wkiol1dk8qesbf--aaaj2r47a3g046.png "/>
See if the value type is not unpacking, it depends on whether he has been replaced with an object or an interface type inherited by a value type ...
Int.tostring This method, the value type is converted to the ValueType type, the boxed condition is not satisfied (you can see the IL code below), you can determine that the int.tostring is not boxed.
. Method private hidebysig static void main (String[] args) cil managed{ .entrypoint // Code size 45 (0x2d ) .maxstack 3 .locals init ([0] int32 v, [1] Object o) il_0000: nop il_0001: ldc.i4.5 il_0002: stloc.0 IL_0003: ldloc.0 IL_0004: box [mscorlib]System.Int32 IL_0009: stloc.1 il_000a: ldloca.s v il_000c: call instance string [mscorlib]system.int32::tostring () IL_0011: ldstr "," IL_0016: ldloc.1 IL_0017: unbox.any &nbsP [mscorlib] System.int32 il_001c: box [mscorlib] system.int32 il_0021: call string [ Mscorlib]system.string::concat (object, object, &nbSp; object) il_0026: call void [ Mscorlib]system.console::writeline (String) IL_002b: nop IL_002c: Ret} // end of method program::main
After understanding the above information, we will know which of the following suggestions:
int num = 3;//with which of the following? Consider string numstr = string. Format ("{0}", num); string numstr = String. Format ("{0}", Num. ToString ());
Reference:
Https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
Http://www.cnblogs.com/DebugLZQ/archive/2012/09/02/2667835.html
http://q.cnblogs.com/q/44027
http://bbs.csdn.net/topics/360191652
This article is from the "lybing" blog, make sure to keep this source http://lybing.blog.51cto.com/3286625/1790605
Does 2.toString () take place in boxing?