<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 裝箱和取消裝箱(C# 編程指南) 裝箱是將[值類型](https://msdn.microsoft.com/zh-cn/library/s1ax56ch.aspx)轉換為 **object** 類型或由此值類型實現的任何接口類型的過程。當 CLR 對值類型進行裝箱時,會將該值包裝到 System.Object 內部,再將后者存儲在托管堆上。取消裝箱將從對象中提取值類型。裝箱是隱式的;取消裝箱是顯式的。裝箱和取消裝箱的概念是類型系統 C# 統一視圖的基礎,其中任一類型的值都被視為一個對象。 在下面的示例中,將整型變量 i 進行了裝箱并分配給對象 o。 ``` int i = 123; // The following line boxes i. object o = i; ``` 然后,可以將對象 o 取消裝箱并分配給整型變量 i: ``` o = 123; i = (int)o; // unboxing ``` 以下示例演示如何在 C# 中使用裝箱。 ``` // String.Concat example. // String.Concat has many versions. Rest the mouse pointer on // Concat in the following statement to verify that the version // that is used here takes three object arguments. Both 42 and // true must be boxed. Console.WriteLine(String.Concat("Answer", 42, true)); // List example. // Create a list of objects to hold a heterogeneous collection // of elements. List<object> mixedList = new List<object>(); // Add a string element to the list. mixedList.Add("First Group:"); // Add some integers to the list. for (int j = 1; j < 5; j++) { // Rest the mouse pointer over j to verify that you are adding // an int to a list of objects. Each element j is boxed when // you add j to mixedList. mixedList.Add(j); } // Add another string and more integers. mixedList.Add("Second Group:"); for (int j = 5; j < 10; j++) { mixedList.Add(j); } // Display the elements in the list. Declare the loop variable by // using var, so that the compiler assigns its type. foreach (var item in mixedList) { // Rest the mouse pointer over item to verify that the elements // of mixedList are objects. Console.WriteLine(item); } // The following loop sums the squares of the first group of boxed // integers in mixedList. The list elements are objects, and cannot // be multiplied or added to the sum until they are unboxed. The // unboxing must be done explicitly. var sum = 0; for (var j = 1; j < 5; j++) { // The following statement causes a compiler error: Operator // '*' cannot be applied to operands of type 'object' and // 'object'. //sum += mixedList[j] * mixedList[j]); // After the list elements are unboxed, the computation does // not cause a compiler error. sum += (int)mixedList[j] * (int)mixedList[j]; } // The sum displayed is 30, the sum of 1 + 4 + 9 + 16. Console.WriteLine("Sum: " + sum); // Output: // Answer42True // First Group: // 1 // 2 // 3 // 4 // Second Group: // 5 // 6 // 7 // 8 // 9 // Sum: 30 ``` ## 性能 相對于簡單的賦值而言,裝箱和取消裝箱過程需要進行大量的計算。對值類型進行裝箱時,必須分配并構造一個新對象。取消裝箱所需的強制轉換也需要進行大量的計算,只是程度較輕。有關更多信息,請參見[性能](https://msdn.microsoft.com/zh-cn/library/ms173196.aspx)。 ## 裝箱 裝箱用于在垃圾回收堆中存儲值類型。裝箱是[值類型](https://msdn.microsoft.com/zh-cn/library/s1ax56ch.aspx)到 **object** 類型或到此值類型所實現的任何接口類型的隱式轉換。對值類型裝箱會在堆中分配一個對象實例,并將該值復制到新的對象中。 請看以下值類型變量的聲明: ``` int i = 123; ``` 以下語句對變量 i 隱式應用了裝箱操作: ``` // Boxing copies the value of i into object o. object o = i; ``` 此語句的結果是在堆棧上創建對象引用 o,而在堆上則引用 **int** 類型的值。該值是賦給變量 i 的值類型值的一個副本。下圖說明了兩個變量 i 和 o 之間的差異。 ![BoxingConversion 圖](https://i-msdn.sec.s-msft.com/dynimg/IC76458.jpeg "BoxingConversion 圖") 裝箱轉換 還可以像下面的示例一樣執行顯式裝箱,但顯式裝箱從來不是必需的: ``` int i = 123; object o = (object)i; // explicit boxing ``` ## 說明 此示例使用裝箱將整型變量 i 轉換為對象 o。這樣一來,存儲在變量 i 中的值就從 123 更改為 456。該示例表明原始值類型和裝箱的對象使用不同的內存位置,因此能夠存儲不同的值。 ``` class TestBoxing { static void Main() { int i = 123; // Boxing copies the value of i into object o. object o = i; // Change the value of i. i = 456; // The change in i does not effect the value stored in o. System.Console.WriteLine("The value-type value = {0}", i); System.Console.WriteLine("The object-type value = {0}", o); } } /* Output: The value-type value = 456 The object-type value = 123 */ ``` ## 取消裝箱 取消裝箱是從 **object** 類型到[值類型](https://msdn.microsoft.com/zh-cn/library/s1ax56ch.aspx)或從接口類型到實現該接口的值類型的顯式轉換。取消裝箱操作包括: * 檢查對象實例,以確保它是給定值類型的裝箱值。 * 將該值從實例復制到值類型變量中。 下面的語句演示裝箱和取消裝箱兩種操作: ``` int i = 123; // a value type object o = i; // boxing int j = (int)o; // unboxing ``` 下圖演示上述語句的結果。 ![圖:取消裝箱轉換](https://i-msdn.sec.s-msft.com/dynimg/IC122059.jpeg "圖:取消裝箱轉換") 取消裝箱轉換 要在運行時成功取消裝箱值類型,被取消裝箱的項必須是對一個對象的引用,該對象是先前通過裝箱該值類型的實例創建的。嘗試取消裝箱 **null** 會導致 [NullReferenceException](https://msdn.microsoft.com/zh-cn/library/system.nullreferenceexception.aspx)。嘗試取消裝箱對不兼容值類型的引用會導致 [InvalidCastException](https://msdn.microsoft.com/zh-cn/library/system.invalidcastexception.aspx)。 下面的示例演示無效的取消裝箱及引發的 **InvalidCastException**。使用 **try** 和 **catch**,在發生錯誤時顯示錯誤信息。 ``` class TestUnboxing { static void Main() { int i = 123; object o = i; // implicit boxing try { int j = (short)o; // attempt to unbox System.Console.WriteLine("Unboxing OK."); } catch (System.InvalidCastException e) { System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message); } } } ``` 此程序輸出: Specified cast is not valid.Error: Incorrect unboxing. 如果將下列語句: ``` int j = (short) o; ``` 更改為: ``` int j = (int) o; ``` 將執行轉換,并將得到以下輸出: Unboxing OK. ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-cn/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 相關章節 更多相關信息: * [引用類型](https://msdn.microsoft.com/zh-cn/library/490f96s2.aspx) * [值類型](https://msdn.microsoft.com/zh-cn/library/s1ax56ch.aspx) ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-cn/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看