C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法
在 C# 中,數(shù)組實(shí)際上是對象,而不只是像 C 和 C++ 中那樣的可尋址連續(xù)內(nèi)存區(qū)域。 Array 是所有數(shù)組類型的抽象基類型。 可以使用 Array 具有的屬性以及其他類成員。 這種用法的一個(gè)示例是使用 Length 屬性來獲取數(shù)組的長度。 下面的代碼將 numbers 數(shù)組的長度(為 5)賦給名為 lengthOfNumbers 的變量:
int[] numbers = { 1, 2, 3, 4, 5 }; int lengthOfNumbers = numbers.Length;
Array 類提供了許多其他有用的方法和屬性,用于排序、搜索和復(fù)制數(shù)組。
示例
此示例使用 Rank 屬性來顯示數(shù)組的維數(shù)。
class TestArraysClass { static void Main() { // Declare and initialize an array: int[,] theArray = new int[5, 10]; System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank); } }
輸出:
The array has 2 dimensions.
使用 ref 和 out 傳遞數(shù)組
與所有 out 參數(shù)一樣,在使用數(shù)組類型的 out 參數(shù)前必須先為其賦值;即必須由被調(diào)用方為其賦值。例如:
static void TestMethod1(out int[] arr) { arr = new int[10]; // definite assignment of arr }
與所有 ref 參數(shù)一樣,數(shù)組類型的 ref 參數(shù)必須由調(diào)用方明確賦值。因此,不需要由被調(diào)用方明確賦值??梢詫?shù)組類型的 ref 參數(shù)更改為調(diào)用的結(jié)果。例如,可以為數(shù)組賦以 null 值,或?qū)⑵涑跏蓟癁榱硪粋€(gè)數(shù)組。例如:
static void TestMethod2(ref int[] arr) { arr = new int[10]; // arr initialized to a different array }
下面兩個(gè)示例演示了 out 與 ref 在將數(shù)組傳遞給方法時(shí)的用法差異。
在此示例中,在調(diào)用方(Main 方法)中聲明數(shù)組 theArray,并在 FillArray 方法中初始化此數(shù)組。然后,數(shù)組元素將返回調(diào)用方并顯示。
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(); } }
輸出:
Array elements are: 1 2 3 4 5
在此示例中,在調(diào)用方(Main 方法)中初始化數(shù)組 theArray,并通過使用 ref 參數(shù)將其傳遞給 FillArray 方法。在 FillArray 方法中更新某些數(shù)組元素。然后,數(shù)組元素將返回調(diào)用方并顯示。
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(); } }
輸出:
Array elements are: 1111 2 3 4 5555
上一篇:解析C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù)
欄 目:C#教程
下一篇:理解C#中參數(shù)的值和引用以及傳遞結(jié)構(gòu)和類引用的區(qū)別
本文標(biāo)題:C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6748.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時(shí)間
- 01-10C#編程實(shí)現(xiàn)自定義熱鍵的方法
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#中DataGridView常用操作實(shí)例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載