日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ToString() 会发生装箱吗?

發布時間:2025/3/21 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ToString() 会发生装箱吗? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近被問到了這個問題,我當時回答是會的,因為ToString()會把值類型轉換成引用類型,所以會發生裝箱。


后來總覺有些不妥當,所以查閱一些資料并參考網絡上的討論:


拆箱裝箱的官方解釋:

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. 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 treated as an object.


裝箱用于在托管內存中存儲值類型。?裝箱是是值類型到?object?類型或到此值類型所實現的任何接口類型的隱式轉換。?對值類型裝箱會在堆中分配一個對象實例,并將該值復制到新的對象中。


下面是官方的幾個拆箱/裝箱的例子:

eg1: int?i?=?123; object?o?=?i;//隱式裝箱eg2: String.Concat("Answer",?42,?true)?//42和true都會發生裝箱eg3: List<object>?mixedList?=?new?List<object>(); mixedList.Add("First?Group:"); for?(int?j?=?1;?j?<?5;?j++) {mixedList.Add(j);//在添加時,j先裝箱 } var?sum?=?0;for?(var?j?=?1;?j?<?5;?j++) {//下面的一行會發生編譯錯誤:?//Operator?'*'?cannot?be?applied?to?operands?of?type?'object'?and?'object'.?//sum?+=?mixedList[j]?*?mixedList[j]);//下面經過拆箱就不會出現上面的編譯錯誤.sum?+=?(int)mixedList[j]?*?(int)mixedList[j]; }

Note:?

相對于簡單的賦值而言,裝箱和取消裝箱過程需要進行大量的計算。?對值類型進行裝箱時,必須分配并構造一個新對象。?取消裝箱所需的強制轉換也需要進行大量的計算,只是程度較輕。

更多性能的了解:https://msdn.microsoft.com/zh-cn/library/ms173196.aspx


再引入圖片來在說明內存中的變化:

int?i?=?123; object?o?=?i;//隱式裝箱


int?i?=?123;??????//?a?value?type object?o?=?i;?????//?boxing int?j?=?(int)o;???//?unboxing


看值類型有沒有進行拆箱,就看他有沒有裝換成Object或者值類型所繼承的接口類型...

Int.ToString 此方法中,值類型轉換成ValueType類型,不滿足裝箱的條件(可以查看下面IL代碼),可以判定Int.ToString是沒有裝箱的。


.method?private?hidebysig?static?void??Main(string[]?args)?cil?managed {.entrypoint//?代碼大小???????45?(0x2d).maxstack??3.locals?init?([0]?int32?v,?[1]?object?o)IL_0000:??nopIL_0001:??ldc.i4.5IL_0002:??stloc.0IL_0003:??ldloc.0IL_0004:??box????????[mscorlib]System.Int32IL_0009:??stloc.1IL_000a:??ldloca.s???vIL_000c:??call???????instance?string?[mscorlib]System.Int32::ToString()IL_0011:??ldstr??????","IL_0016:??ldloc.1IL_0017:??unbox.any??[mscorlib]System.Int32IL_001c:??box????????[mscorlib]System.Int32IL_0021:??call???????string?[mscorlib]System.String::Concat(object,object,object)IL_0026:??call???????void?[mscorlib]System.Console::WriteLine(string)IL_002b:??nopIL_002c:??ret }?//?end?of?method?Program::Main


了解了上面的信息之后以后就知道下面建議用哪一個了吧:

int?num?=?3; //用下面的哪個呢?請思考 string?numStr?=?string.Format("{0}",?num); string?numStr?=?string.Format("{0}",?num.ToString());


參考:

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

轉載于:https://blog.51cto.com/lybing/1790605

總結

以上是生活随笔為你收集整理的ToString() 会发生装箱吗?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。