c#学习体会:使用 ref 和 out 传递数组(downmoon)
2007-12-28 14:20
453 查看
c#学习体会:使用 ref 和 out 传递数组(downmoon),希望与大家分享
1、与所有的 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由接受方为其赋值。例如:
public static void MyMethod(out int[] arr)
public static void MyMethod(ref int[] arr)
using System;
class TestOut
using System;
class TestRef
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void FillArray(ref int[] arr)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 根据需要创建一新的数组(不是必须的)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (arr == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr = new int[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 否则填充数组,就可以了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr[0] = 123;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr[4] = 1024;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static public void Main ()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//初始化数组:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int[] myArray = ...{1,2,3,4,5};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 使用ref传递数组:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FillArray(ref myArray);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//显示更新后的数组元素:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("数组元素是:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < myArray.Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(myArray[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
输出
数组元素是:
123
2
3
4
1024
1、与所有的 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由接受方为其赋值。例如:
public static void MyMethod(out int[] arr)
public static void MyMethod(ref int[] arr)
using System;
class TestOut
using System;
class TestRef
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void FillArray(ref int[] arr)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 根据需要创建一新的数组(不是必须的)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (arr == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr = new int[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 否则填充数组,就可以了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr[0] = 123;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr[4] = 1024;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static public void Main ()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//初始化数组:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
int[] myArray = ...{1,2,3,4,5};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 使用ref传递数组:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FillArray(ref myArray);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//显示更新后的数组元素:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("数组元素是:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < myArray.Length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine(myArray[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
输出
数组元素是:
123
2
3
4
1024
相关文章推荐
- c#学习体会:使用 ref 和 out 传递数组(downmoon)
- c#学习体会:使用 ref 和 out 传递数组(downmoon)
- c#学习体会:使用 ref 和 out 传递数组(downmoon)
- c#学习体会:使用 ref 和 out 传递数组(downmoon)
- c#学习体会:使用 ref 和 out 传递数组
- c#学习体会:使用 ref 和 out 传递数组
- 使用 ref 和 out 传递数组(C# 编程指南)
- 使用 ref 和 out 传递数组(C# 编程指南)
- C#中使用ref和out传递数组的方法
- 使用 ref 和 out 传递数组(C# 编程指南)
- c# ---使用 ref 和 out 传递数组
- 【转】使用 ref 和 out 传递数组(C# 编程指南)
- C# 使用 ref 和 out 传递数组
- C#中使用 ref 和 out 传递数组
- C#方法参数传递-同时使用ref和out关键字
- 使用 ref 和 out 传递数组
- C#Trick&Tip: 使用 ref 与 out 传递参数的区别
- C#基础学习--方法参数的种类,值参数,引用参数(ref),输出参数(out),数组参数(params)
- 使用 ref 和 out 传递数组
- 使用 ref 和 out 传递数组