ref 和out的使用还有区别
来源:互联网 发布:moneywiz2 mac 编辑:程序博客网 时间:2024/06/07 00:48
广义的说ref和out都是具有实现多个返回值的功能
区别在于out只进不出 ref又进又出
举个例子 用out返回数组中最大值与最小值
static void MaxAndMin(int[] arr,out int min,out int max)
{
min = arr[0];
max = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (min>arr[i])
{
min = arr[i];
}
if (max< arr[i])
{
max = arr[i];
}
}
}
在Main方法中调用
int[] arr = { 1, 5, 8, 4, 20, 60, 13 };
int min = 0;
int max = 0;
MaxAndMin(arr,out min,out max);
Console.WriteLine(min+” “+max);
static void IndexOf(int[] arr,ref int index)
{
//index没有进行初始化 最后还能传出去
int count = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == index)
{
index = i;
break;
}
else {
count++;
}
}
if (count==arr.Length)
{
index = -1;
}
}
在Main方法中调用
int[] arr = { 1, 5, 8, 4, 20, 60, 13 };
int index = 8;
IndexOf(arr, ref index);
Console.WriteLine(index);
- ref 和out的使用还有区别
- ref 和 out 的使用区别
- ref和out的使用与区别
- ref 和 out 的详解 区别 使用
- ref和out的使用与区别
- ref和out的使用与区别
- ref和out的使用与区别
- ref和out的使用与区别
- ref和out的使用与区别
- ref和out的区别
- ref 和 out 的区别
- out和ref的区别
- ref和out的区别
- ref和out的区别
- ref和out的区别
- ref 和 out 的区别
- Ref和Out的区别
- out和ref的区别?
- Java获取当前年份,计算当前月有多少自然日、有多少工作日、有几周
- python 生成器
- (0068)iOS开发之AutoLayout框架Masonry使用心得
- 唯品会前端面试
- Windows下用ndk编译ffmpeg
- ref 和out的使用还有区别
- 判断App版本 startfrom
- 智能指针
- 在eclipse中配置maven
- Activity transition(Activity转场动画效果)
- 支持移动端深度学习的几种开源框架
- js数组深复制和浅复制的区别
- 为什么模板不支持分离编译
- java对象的序列化和反序列化