來源:互聯網網民 2007-01-29 12:06:45
評論c#學習體會:使用 ref 和 out 傳遞數組(downmoon),希望與大家分享
1、與所有的 out 參數一樣,在使用數組類型的 out 參數前必須先爲其賦值,即必須由接受方爲其賦值。例如:
public static void MyMethod(out int[] arr)
...{
arr = new int[10]; // 數組arr的明確委派
}
2、與所有的 ref 參數一樣,數組類型的 ref 參數必須由調用方明確賦值。因此不需要由接受方明確賦值。可以將數組類型的 ref 參數更改爲調用的結果。例如,可以爲數組賦以 null 值,或將其初始化爲另一個數組。例如:
public static void MyMethod(ref int[] arr)
...{
arr = new int[10]; // arr初始化爲一個新的數組
}
下面的兩個示例說明 out 和 ref 在將數組傳遞給方法上的用法差異。
示例 1
在此例中,在調用方(Main 方法)中聲明數組 myArray,並在 FillArray 方法中初始化此數組。然後將數組元素返回調用方並顯示。
using System;
class TestOut
...{
static public void FillArray(out int[] myArray)
...{
// 初始化數組(必須):
myArray = new int[5] ...{1, 2, 3, 4, 5};
}
static public void Main()
...{
int[] myArray; // 初始化數組(不是必須的!)
// 傳遞數組給(使用out方式的)調用方:
FillArray(out myArray);
// 顯示數組元素
Console.WriteLine("數組元素是:");
for (int i=0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
輸出
數組元素是:
1
2
3
4
5
示例 2
在此例中,在調用方(Main 方法)中初始化數組 myArray,並通過使用 ref 參數將其傳遞給 FillArray 方法。在 FillArray 方法中更新某些數組元素。然後將數組元素返回調用方並顯示。
using System;
class TestRef
...{
public static void FillArray(ref int[] arr)
...{
// 根據需要創建一新的數組(不是必須的)
if (arr == null)
arr = new int[10];
// 否則填充數組,就可以了
arr[0] = 123;
arr[4] = 1024;
}
static public void Main ()
...{
//初始化數組:
int[] myArray = ...{1,2,3,4,5};
// 使用ref傳遞數組:
FillArray(ref myArray);
//顯示更新後的數組元素:
Console.WriteLine("數組元素是:");
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
輸出
數組元素是:
123
2
3
4
1024
c#學習體會:使用 ref 和 out 傳遞數組(downmoon),希望與大家分享
1、與所有的 out 參數一樣,在使用數組類型的 out 參數前必須先爲其賦值,即必須由接受方爲其賦值。例如:
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330342183090.jpg[/img][/url]
public static void MyMethod(out int[] arr)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330342183164.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350714988.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715059.jpg[/img][/url]
arr = new int[10]; // 數組arr的明確委派
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715128.jpg[/img][/url]}
2、與所有的 ref 參數一樣,數組類型的 ref 參數必須由調用方明確賦值。因此不需要由接受方明確賦值。可以將數組類型的 ref 參數更改爲調用的結果。例如,可以爲數組賦以 null 值,或將其初始化爲另一個數組。例如:
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715441.jpg[/img][/url]
public static void MyMethod(ref int[] arr)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715489.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715739.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715832.jpg[/img][/url]
arr = new int[10]; // arr初始化爲一個新的數組
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350715898.jpg[/img][/url]}
下面的兩個示例說明 out 和 ref 在將數組傳遞給方法上的用法差異。
示例 1
在此例中,在調用方(Main 方法)中聲明數組 myArray,並在 FillArray 方法中初始化此數組。然後將數組元素返回調用方並顯示。
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716236.jpg[/img][/url]
using System;
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716298.jpg[/img][/url]class TestOut
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716513.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716569.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716624.jpg[/img][/url]static public void FillArray(out int[] myArray)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716861.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716917.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350716973.jpg[/img][/url]// 初始化數組(必須):
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350717316.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350717365.jpg[/img][/url]
myArray = new int[5] ...{1, 2, 3, 4, 5};
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350717420.jpg[/img][/url]}
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350717674.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350717722.jpg[/img][/url]
static public void Main()
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350717789.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718094.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718151.jpg[/img][/url]
int[] myArray; // 初始化數組(不是必須的!)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718199.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718463.jpg[/img][/url]
// 傳遞數組給(使用out方式的)調用方:
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718526.jpg[/img][/url]
FillArray(out myArray);
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718582.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718840.jpg[/img][/url]
// 顯示數組元素
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718902.jpg[/img][/url]
Console.WriteLine("數組元素是:");
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350718960.jpg[/img][/url]for (int i=0; i < myArray.Length; i++)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350719207.jpg[/img][/url]
Console.WriteLine(myArray[i]);
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350719268.jpg[/img][/url]
}
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350719324.jpg[/img][/url]}
輸出
數組元素是:
1
2
3
4
5
示例 2
在此例中,在調用方(Main 方法)中初始化數組 myArray,並通過使用 ref 參數將其傳遞給 FillArray 方法。在 FillArray 方法中更新某些數組元素。然後將數組元素返回調用方並顯示。
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350719572.jpg[/img][/url]
using System;
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350719628.jpg[/img][/url]class TestRef
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350720191.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350720247.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350720296.jpg[/img][/url]public static void FillArray(ref int[] arr)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350720769.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350720818.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350720874.jpg[/img][/url]// 根據需要創建一新的數組(不是必須的)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350721342.jpg[/img][/url]
if (arr == null)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350721398.jpg[/img][/url]
arr = new int[10];
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350721454.jpg[/img][/url]// 否則填充數組,就可以了
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350721988.jpg[/img][/url]
arr[0] = 123;
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350722037.jpg[/img][/url]
arr[4] = 1024;
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350722093.jpg[/img][/url]}
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350722550.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350722599.jpg[/img][/url]
static public void Main ()
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350722661.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723123.jpg[/img][/url]
...{
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723179.jpg[/img][/url]
//初始化數組:
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723240.jpg[/img][/url][url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723519.jpg[/img][/url]
int[] myArray = ...{1,2,3,4,5};
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723581.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723644.jpg[/img][/url]// 使用ref傳遞數組:
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350723976.jpg[/img][/url]
FillArray(ref myArray);
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724034.jpg[/img][/url]
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724082.jpg[/img][/url]//顯示更新後的數組元素:
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724401.jpg[/img][/url]
Console.WriteLine("數組元素是:");
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724463.jpg[/img][/url]
for (int i = 0; i < myArray.Length; i++)
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724521.jpg[/img][/url]Console.WriteLine(myArray[i]);
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724792.jpg[/img][/url]
}
[url=/bbs/detail_836500.html][img]http://image2.wangchao.net.cn/bbs/1330350724849.jpg[/img][/url]
}
輸出
數組元素是:
123
2
3
4
1024