首页 > 其他分享 >总结ref和out的区别

总结ref和out的区别

时间:2022-11-10 07:44:58浏览次数:62  
标签:总结 Console int void Program ref out

之前每次遇到ref和out时,老是忘记他们的使用方法和区别。每次都要网上搜一下别人写的博客来回忆。这次干脆自己整合一下别人博客的内容,方便下次忘记时查询。

用途:

 在C#中通过使用方法来获取返回值时,通常只能得到一个返回值。因此,当一个方法需要返回多个值的时候,就需要用到ref和out。

 

概述:

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

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

 

同:

1、都能返回多个返回值。

2、若要使用 ref 和out参数,则方法定义和调用方法都必须显式使用 ref和out 关键字。在方法中对参数的设置和改变将会直接影响函数调用之处(参数的初始值)。

 

异:

1、ref指定的参数在函数调用时候必须初始化,不能为空的引用。而out指定的参数在函数调用时候可以不初始化;

2、out指定的参数在进入函数时会清空自己,必须在函数内部赋初值。而ref指定的参数不需要。

 

口诀:

ref有进有出,out只出不进。

正确的使用ref:

class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x = 10;
            int y = 20;
            pg.GetValue(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);
 
            Console.ReadLine();
        }
 
        public void GetValue(ref int x, ref int y)
        {
            x = 521;
            y = 520;
        }
    }

运行结果为 

x=521,y=520

错误的使用ref:

class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x;
            int y;        //此处x,y没有进行初始化,则编译不通过。
            pg.GetValue(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);
 
            Console.ReadLine();
        }
 
        public void GetValue(ref int x, ref int y)
        {
            x = 1000;
            y = 1;
        }    
    }

  

正确的使用out:

class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x=10;
            int y=233;
            pg.Swap(out x, out y);
            Console.WriteLine("x={0},y={1}", x, y);
 
            Console.ReadLine();
        }
 
        public void Swap(out int a,out  int b)
        {
            int temp = a;   //a,b在函数内部没有赋初值,则出现错误。
            a = 521;
            b = 520;
        }     
    }

  

运行结果为 

x=521,y=520

错误的使用out:

class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x=10;
            int y=233;
            pg.Swap(out x, out y);
            Console.WriteLine("x={0},y={1}", x, y);
 
            Console.ReadLine();
        }
 
        public void Swap(out int a,out  int b)
        {
            int temp = a;   //a,b在函数内部没有赋初值,则出现错误。
            a = b;
            b = temp;
        }     
    }

  

原文连接

https://www.cnblogs.com/wolf-sun/p/3371174.html

https://www.cnblogs.com/yanhan/archive/2013/01/26/2877889.html

标签:总结,Console,int,void,Program,ref,out
From: https://www.cnblogs.com/hushzhang/p/16875813.html

相关文章

  • react高频面试题总结(附答案)
    hooks为什么不能放在条件判断里以setState为例,在react内部,每个组件(Fiber)的hooks都是以链表的形式存在memoizeState属性中update阶段,每次调用setState,链表......
  • Spring Security 知识点总结
    Security部分WebSecurityConfigurerAdaptersecurity配置的核心类在这里配置权限等信息authenticationauthentication是认证(登陆)authorizationauthorizati......
  • Spring Security 知识点总结
    Security部分WebSecurityConfigurerAdaptersecurity配置的核心类在这里配置权限等信息authenticationauthentication是认证(登陆)authorizationauthorizati......
  • How to Find Out What Linux Distribution You Are Using
    HowtoknownwhatLinuxDistributionIamusingnow.Allright,runthiscommandandyoucouldgetwhatyouneedinfromtheoutputresult19:12:17-androidyue~......
  • Issues About Installing Octopress
    ActuallyIamfreshtoWriteBlogwithOctopressinGithubPages.Accordingtothe​​Introduction​​.AndnowIhavemakeitavaibleandthewebsiteispresen......
  • CSP-S2022 总结
    调整了下心态开考顺序开题看完\(T1,T2\)直接开打\(T2\)的线段树,还是比较好写的然后思考先打\(T1\)呢还是拍\(T2\),最后决定拍\(T2\),稳一点发现随机数据很弱,决定......
  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • 第五小组chap 7学习总结
     什么是数组呢? 顾名思义数组就是很多数的组合!那么这些数有没有什么要求呢,是不是不管什么数组合在一起都是数组呢?同一数组中存储的数必须满足以下两个条件: 1.这些数......
  • 11.9总结
    11.9GZEZNOIP2022模拟测试赛(五十七)PROBLEMA:[精神焕发(huan)]题面描述:给你一颗树,每个点可以回血,经过一次回血\(w[i]\)(有正有负)\(q\)次询问:给出\(x,s,t\)......
  • javascript初识以及总结
    今天主要学习了js的概述,书写的三种位置(和css一样内嵌外联和内联三种)以及变量的定义,变量的定义用的是var来定义的并且学了五种数据类型 有number数值型strin......