首页 > 代码库 > 引用参数,值参数,ref,out
引用参数,值参数,ref,out
1,一个参数只有在引用的时候才能改变其值,这是一种情况
2,一个参数在引用后要永久的改变其值(可以用返回参数的形式)
3,多个参数在引用后要永久的改变其值或者多个参数中的部分(返回参数就适合了,因为只能返回一个,所以就用REF和4.OUT了)
上例子
1
static void ShowDouble(int val){val *= 2;Console.WriteLine("val doubled = {0}", val);}int myNumber = 5;Console.WriteLine("myNumber = {0}", myNumber);ShowDouble(myNumber);Console.WriteLine("myNumber = {0}", myNumber);
2
static int DoubleNum(int val){val *= 2;return val;}int myNumber = 5;Console.WriteLine("myNumber = {0}", myNumber);myNumber = DoubleNum(myNumber);Console.WriteLine("myNumber = {0}", myNumber);
3。REF
static void ShowDouble(ref int val){val *= 2;Console.WriteLine("val doubled = {0}", val);}int myNumber = 5;Console.WriteLine("myNumber = {0}", myNumber);ShowDouble(ref myNumber);Console.WriteLine("myNumber = {0}", myNumber);
4. OUT
static int MaxValue(int[] intArray, out int maxIndex){int maxVal = intArray[0];maxIndex = 0;for (int i = 1; i < intArray.Length; i++){if (intArray[i] > maxVal){maxVal = intArray[i];maxIndex = i;}}return maxVal;}int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };int maxIndex;Console.WriteLine("The maximum value in myArray is {0}",MaxValue(myArray, out maxIndex));Console.WriteLine("The first occurrence of this value is at element {0}",maxIndex + 1);
引用参数,值参数,ref,out
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。