C#:使用ref,out关键词定义返回值函数

简介: 代码 static int xValue(ref int num, ref int num2)        {            num = 2 * num;            num2 = 2 * num2;            return num;        }   ...
img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
static   int  xValue( ref   int  num,  ref   int  num2)
        {
            num 
=   2   *  num;
            num2 
=   2   *  num2;
            
return  num;
        }
        
static   void  Main( string [] args)
        {
            
int  num  =   5 ;
            
int  num2  =   10 ;
            Console.WriteLine(xValue(
ref  num,  ref  num2));
            Console.WriteLine(num 
+   " : "   +  num2);
            
// 这时的num=10, num2=20;输出结果:10:20;加上ref后会影响到函数外面的变量
        }

两个注意点:
1:定义函数时.指定参数的数据类型与函数体外面定义的变量类型一致,static int xValue(ref int num, ref int num2)
2:使用此函数时为每个参数加上ref.如果不加的话则不会影响函数外面变量的值

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
static   int  MaxValueL( int [] numArry,  out   int  maxIndex,  out   int  maxIndex2)
        {
            
int  MaxValue  =  numArry[ 0 ];
            maxIndex 
=   0 ;
            maxIndex2 
=   100 ;
            
for  ( int  i  =   1 ; i  <  numArry.Length; i ++ )
            {
                
if  (MaxValue  <  numArry[i])
                {
                    MaxValue 
=  numArry[i];
                    maxIndex 
=  i;
                }
            }
            
return  MaxValue;
        }
        
static   void  Main( string [] args)
        {
            
int  maxIndex;
            
int  maxIndex2;
            
int [] numArr  =  {  14 3 34 11 99 33  };
            Console.WriteLine(MaxValueL(numArr, 
out  maxIndex,  out  maxIndex2));
            
// 返回函数体内的变量MaxValue
            Console.WriteLine(maxIndex  +   1 );
            
// 返回函数体内的变量maxIndex
            Console.WriteLine(maxIndex2);
            
// 返回函数体内的变量maxIndex2
            Console.ReadKey();
        }

 

两个注意点:
1:定义函数时.指定输出参数的数据类型与函数体外面定义的变量类型一致,out int maxIndex, out int maxIndex2
2:使用此函数时为想要输出参数加上 out .
这里的输出是指把函数体内的变量输出到函数体外应用.这也关联到了变量的作用域问题.

 

目录
相关文章
|
1月前
|
存储 C#
C#学习系列相关之数组(一)---数组的定义与使用
C#学习系列相关之数组(一)---数组的定义与使用
|
1月前
|
C#
C#系列之ref和out的区别
C#系列之ref和out的区别
|
1月前
|
存储 C# 索引
C#学习相关系列之数据类型类的定义(一)
C#学习相关系列之数据类型类的定义(一)
|
3月前
|
存储 C#
C# 方法详解:定义、调用、参数、默认值、返回值、命名参数、方法重载全解析
方法是一段代码,只有在调用时才会运行。 您可以将数据(称为参数)传递给方法。 方法用于执行某些操作,也被称为函数。 为什么使用方法?为了重用代码:定义一次代码,然后多次使用。
43 0
|
8月前
|
存储 C# C++
从C++角度讲解C#Out和ref的区别
从C++角度讲解C#Out和ref的区别
|
7月前
|
C#
C#中out和ref之间的区别
C#中out和ref之间的区别
|
9月前
|
C#
C#基础⑧——方法(函数、重载、out、ref)
比喻成职能。比喻成一个生产自行车老板,一个地方专门放螺丝,一个地方专门放轮,一个地方专门放车链子,需要组装什么就从那个仓库里面拿就行了。各司其职。
|
关系型数据库 MySQL C#
【C#】【MySQL】【GridView】删除出现Parameter index is out of range
【C#】【MySQL】【GridView】删除出现Parameter index is out of range
94 0
【C#】【MySQL】【GridView】删除出现Parameter index is out of range
|
存储 JavaScript 前端开发
C#(.NET)面试题:做一个能自定定义输入命令的表格程序
C#(.NET)面试题:做一个能自定定义输入命令的表格程序
110 0
C#(.NET)面试题:做一个能自定定义输入命令的表格程序
C# ref out的使用与区别
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。Ref型参数引入前必须赋值。 out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字。 Out型参数引入前不需赋值,赋值也没用。
123 0
C# ref out的使用与区别