<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 將數組作為參數傳遞(C# 編程指南) 數組可作為實參傳遞給方法形參。由于數組是引用類型,因此方法可以更改元素的值。 ## 將一維數組作為參數傳遞 可以將初始化的一維數組傳遞給方法。例如,下面的語句將數組發送到 print 方法。 ``` int[] theArray = { 1, 3, 5, 7, 9 }; PrintArray(theArray); ``` 下面的代碼顯示 print 方法的部分實現。 ``` void PrintArray(int[] arr) { // Method code. } ``` 您可以在一個步驟中初始化和傳遞新數組,如下面的示例所示。 ``` PrintArray(new int[] { 1, 3, 5, 7, 9 }); ``` ## 示例 ### 說明 在下面的示例中,將初始化一個字符串數組并將其作為參數傳遞到字符串的 PrintArray 方法。該方法顯示數組的元素。接下來,調用 ChangeArray 和 ChangeArrayElement 方法以演示通過值發送數組參數時不會阻止更改這些數組元素。 ### 代碼 ``` class ArrayClass { static void PrintArray(string[] arr) { for (int i = 0; i < arr.Length; i++) { System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : ""); } System.Console.WriteLine(); } static void ChangeArray(string[] arr) { // The following attempt to reverse the array does not persist when // the method returns, because arr is a value parameter. arr = (arr.Reverse()).ToArray(); // The following statement displays Sat as the first element in the array. System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]); } static void ChangeArrayElements(string[] arr) { // The following assignments change the value of individual array // elements. arr[0] = "Sat"; arr[1] = "Fri"; arr[2] = "Thu"; // The following statement again displays Sat as the first element // in the array arr, inside the called method. System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]); } static void Main() { // Declare and initialize an array. string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; // Pass the array as an argument to PrintArray. PrintArray(weekDays); // ChangeArray tries to change the array by assigning something new // to the array in the method. ChangeArray(weekDays); // Print the array again, to verify that it has not been changed. System.Console.WriteLine("Array weekDays after the call to ChangeArray:"); PrintArray(weekDays); System.Console.WriteLine(); // ChangeArrayElements assigns new values to individual array // elements. ChangeArrayElements(weekDays); // The changes to individual elements persist after the method returns. // Print the array, to verify that it has been changed. System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:"); PrintArray(weekDays); } } // Output: // Sun Mon Tue Wed Thu Fri Sat // arr[0] is Sat in ChangeArray. // Array weekDays after the call to ChangeArray: // Sun Mon Tue Wed Thu Fri Sat // // arr[0] is Sat in ChangeArrayElements. // Array weekDays after the call to ChangeArrayElements: // Sat Fri Thu Wed Thu Fri Sat ``` ## 將多維數組作為參數傳遞 可采用與傳遞一維數組相同的方式將初始化的多維數組傳遞給方法。 ``` int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } }; Print2DArray(theArray); ``` 下面的代碼顯示 print 方法的部分聲明,該方法接受一個二維數組作為其參數。 ``` void Print2DArray(int[,] arr) { // Method code. } ``` 您可以在一個步驟中初始化和傳遞新數組,如下面的示例所示。 ``` Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); ``` ## 示例 ### 說明 在下面的示例中,將初始化一個二維整數數組并將其傳遞到 Print2DArray 方法。該方法顯示數組的元素。 ### 代碼 ``` class ArrayClass2D { static void Print2DArray(int[,] arr) { // Display the array elements. for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]); } } } static void Main() { // Pass the array as an argument. Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: Element(0,0)=1 Element(0,1)=2 Element(1,0)=3 Element(1,1)=4 Element(2,0)=5 Element(2,1)=6 Element(3,0)=7 Element(3,1)=8 */ ``` ## 請參閱 [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>

                              哎呀哎呀视频在线观看