<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 使用 ref 和 out 傳遞數組(C# 編程指南) 與所有 [out](https://msdn.microsoft.com/zh-cn/library/t3c3bfhx.aspx) 參數一樣,在使用數組類型的 **out** 參數前必須先為其賦值;即必須由被調用方為其賦值。例如: ``` static void TestMethod1(out int[] arr) { arr = new int[10]; // definite assignment of arr } ``` 與所有 [ref](https://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx) 參數一樣,數組類型的 **ref** 參數必須由調用方明確賦值。因此,不需要由被調用方明確賦值。可以將數組類型的 **ref** 參數更改為調用的結果。例如,可以為數組賦以 [null](https://msdn.microsoft.com/zh-cn/library/edakx9da.aspx) 值,或將其初始化為另一個數組。例如: ``` static void TestMethod2(ref int[] arr) { arr = new int[10]; // arr initialized to a different array } ``` 下面兩個示例演示了 **out** 與 **ref** 在將數組傳遞給方法時的用法差異。 在此示例中,在調用方(Main 方法)中聲明數組 theArray,并在 FillArray 方法中初始化此數組。然后,數組元素將返回調用方并顯示。 ``` class TestOut { static void FillArray(out int[] arr) { // Initialize the array: arr = new int[5] { 1, 2, 3, 4, 5 }; } static void Main() { int[] theArray; // Initialization is not required // Pass the array to the callee using out: FillArray(out theArray); // Display the array elements: System.Console.WriteLine("Array elements are:"); for (int i = 0; i < theArray.Length; i++) { System.Console.Write(theArray[i] + " "); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: Array elements are: 1 2 3 4 5 */ ``` 在此示例中,在調用方(Main 方法)中初始化數組 theArray,并通過使用 **ref** 參數將其傳遞給 FillArray 方法。在 FillArray 方法中更新某些數組元素。然后,數組元素將返回調用方并顯示。 ``` class TestRef { static void FillArray(ref int[] arr) { // Create the array on demand: if (arr == null) { arr = new int[10]; } // Fill the array: arr[0] = 1111; arr[4] = 5555; } static void Main() { // Initialize the array: int[] theArray = { 1, 2, 3, 4, 5 }; // Pass the array using ref: FillArray(ref theArray); // Display the updated array: System.Console.WriteLine("Array elements are:"); for (int i = 0; i < theArray.Length; i++) { System.Console.Write(theArray[i] + " "); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: Array elements are: 1111 2 3 4 5555 */ ``` ## 請參閱 [ref(C# 參考)](https://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx) [out 參數修飾符(C# 參考)](https://msdn.microsoft.com/zh-cn/library/ee332485.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [數組(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/9b9dty7d.aspx) [一維數組(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/0a7fscd0.aspx) [多維數組(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/2yd9wwz4.aspx) [交錯數組(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/2s05feca.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>

                              哎呀哎呀视频在线观看