C#中ref和out的区别浅析

这篇文章主要介绍了C#中ref和out的区别浅析,当一个方法须要返回多个值的时候,就须要用到ref和out,那么这两个方法区别在哪儿呢,须要的朋友能够参考下
 

在C#中经过使用方法来获取返回值时,一般只能获得一个返回值。所以,当一个方法须要返回多个值的时候,就须要用到ref和out,那么这两个方法区别在哪儿呢?c#

MSDN:
       ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所作的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。
      out 关键字会致使参数经过引用来传递。这与 ref 关键字相似,不一样之处在于 ref 要求变量必须在传递以前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字。数组

案例:spa

      定义一个方法,求一个整数数组中的最大值,最小值,和,平均数。若是是一个方法只能有一个返回值,那只能每个都得定义一个方法来实现,不过有了ref和out这实现起来就方便多了。code

ref:内存

 

复制代码代码以下:

static int GetIntResult(int[] arry, ref float avg, ref int max, ref int min)
        {
            int sum = 0;
            max = arry[0];
            min = arry[0];
            for (int i = 0; i < arry.Length; i++)
            {
                sum += arry[i];
               
                if (max < arry[i])
                {
                    max = arry[i];
                }
                if (min > arry[i])
                {
                    min = arry[i];
                }
            }
            avg = sum / arry.Length;
            return sum;
        }

 

而后在控制台中试着调用该方法:编译器

 

复制代码代码以下:

static void Main(string[] args)
        {
            int[] arr = { 1,2,3,4,5,6,7,8,9};
            float avg;
            int max;
            int min;
            int sum = GetIntResult(arr, ref avg, ref max, ref min);
        }

 

此时编译器就会提示画红线,错误:使用了未赋值的avg,max,minstring

 

复制代码代码以下:

static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            float avg = 0;
            int max = 0;
            int min = 0;
            int sum = GetIntResult(arr, ref avg, ref max, ref min);
            Console.WriteLine("和:{0}\t平均值:{1}\t最大值:{2}\t最小值:{3}", sum, avg, max, min);
            Console.Read();
        }

 

运行结果:it

总结:io

      ref这个关键字告诉c#编译器被传递的参数值指向与调用代码中变量相同的内存。这样,若是被调用的方法修改了这些值而后返回的话,调用代码的变量也就被修改了。编译

      ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所作的任何更改都将反映在该变量中(avg,max,min的初始值为0,调用方法后值改变)。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。

out:

      换成out以后,上面的方法再也不适用,报错,错误 : 控制离开当前方法以前必须对 out 参数“min”和"max"赋值。你会发现这里max和min在循环外并未初始化。因此才会出错。

修改后代码:

复制代码代码以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

 

namespace Wolfy.RefAndOut
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            float avg;//在使用out关键字时,不须要在此处初始化,初始化也不会影响到方法内部的值,因此你初始化没用
            int max;
            int min;
            int sum = GetIntResult(arr, out avg, out max, out min);
            Console.WriteLine("和:{0}\t平均值:{1}\t最大值:{2}\t最小值:{3}", sum, avg, max, min);
            Console.Read();
        }
        static int GetIntResult(int[] arry, out float avg, out int max, out int min)
        {
            int sum = 0;
            max = arry[0];
            min = arry[0];//使用out关键字时,必须在离开方法前对out关键字修饰的参数初始化
            for (int i = 0; i < arry.Length; i++)
            {
                sum += arry[i];
               
                if (max < arry[i])
                {
                    max = arry[i];
                }
                if (min > arry[i])
                {
                    min = arry[i];
                }
            }
            avg = sum / arry.Length;
            return sum;
        }
    }
}

 

   结果和上面同样。

总结:       out 关键字会致使参数经过引用来传递。这与 ref 关键字相似,不一样之处在于 ref 要求变量必须在传递以前进行初始化。若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字。

相关文章
相关标签/搜索