相同点:

  1. ref 和 out 都是按地址传递的,使用后都将改变原来参数的数值;

  2. 方法定义和调用方法都必须显式使用 ref 或者 out关键字;

  3. 通过ref 和 ref 特性,一定程度上解决了C#中的函数只能有一个返回值的问题。

不同点:

  传递到 ref 参数的参数必须初始化,否则程序会报错。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = 1;
int b = 2;
Fun(ref a,ref b);
Console.WriteLine("a:{0},b:{1}", a, b);//输出:3和4说明传入Fun方法是a和b的引用
}
static void Fun(ref int a, ref int b) {
a = 3;
b = 4;
}
}
}

  out关键字无法将参数值传递到out参数所在的方法中, out参数的参数值初始化必须在其方法内进行,否则程序会报错。

using System.Text;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 100;
int b;
Fun(out a, out b);
Console.WriteLine("a:{0},b:{1}", a, b);
} static void Fun(out int a, out int b)
{
//a = 1+2;
if (a == 100)
a = 2; b = 1;
} }
}

代码里红体字a 报错 “Use of unassigned out parameter 'a' ”

下面的代码是正确的。

using System;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 100;
int b;
Fun(out a, out b);
Console.WriteLine("a:{0},b:{1}", a, b);
} static void Fun(out int a, out int b)
{
a = 1+2; b = 1;
} }
}

输出结果为:

注意点:

using System;

namespace ConsoleApplication1
{
class Program
{
public void SampleMethod(ref int i) { }
public void SampleMethod(out int i) { } }
}

上面代码会报错“ 'Program' cannot define an overloaded method that differs only on parameter modifiers 'out' and 'ref'  ”

尽管 ref 和 out 在运行时的处理方式不同,但在编译时的处理方式相同。因此,如果一个方法采用 ref 参数,而另一个方法采用 out 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译上面的代码。

using System;

namespace ConsoleApplication1
{
class Program
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
}

但是,如果一个方法采用 ref 或 out 参数,而另一个方法不采用这两个参数,则可以进行重载。

参考点1   参考点2

      

C# ref and out的更多相关文章

  1. .NET 基础一步步一幕幕[out、ref、params]

    out.ref.params out: 如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组. 但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候, 我们可以考虑使用 ...

  2. out和ref详解

    要想充分理解C# out和ref,必须先明确如下两个概念(对值类型与引用类型掌握比较好的,可以跳过"一.明确两个基本概念") 一.明确两个基本概念 值类型: 定义:通过值的方式来传 ...

  3. c#编程基础之ref、out参数

    引例: 先看这个源码,函数传递后由于传递的是副本所以真正的值并没有改变. 源码如下: using System; using System.Collections.Generic; using Sys ...

  4. C#中out和ref之间的区别【转】

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  5. 通过一个实例重新认识引用类型,值类型,数组,堆栈,ref

    昨天在写代码时候遇到了一个问题,百思不得其解,感觉颠覆了自己对C#基础知识的认知,因为具体的情境涉及公司代码不便放出,我在这里举个例子,先上整个测试所有的代码,然后一一讲解我的思考过程: using ...

  6. 图解C#的值类型,引用类型,栈,堆,ref,out

    C# 的类型系统可分为两种类型,一是值类型,一是引用类型,这个每个C#程序员都了解.还有托管堆,栈,ref,out等等概念也是每个C#程序员都会接触到的概念,也是C#程序员面试经常考到的知识,随便搜搜 ...

  7. 异步方法不能使用ref和out的解决方法

    异常处理汇总-后端系列:http://www.cnblogs.com/dunitian/p/4523006.html 应用场景==>后端现在都是用异步方法,那么分页是必不可少的,于是就有了这个问 ...

  8. [C#]浅析ref、out参数

    转载:http://www.cnblogs.com/vd630/p/4601919.html#top 按引用传递的参数算是C#与很多其他语言相比的一大特色,想要深入理解这一概念应该说不是一件容易的事, ...

  9. C#基础-out与ref字段

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. C# out ref 重载

    今天看极客学院wiki时候看到了out,ref的介绍,之前对这个知识点没有深刻认识,所以就写了个小测试看了下,瞬间明白了. using System; using System.Collections ...

随机推荐

  1. 2019牛客暑期多校训练营(第九场)J Symmetrical Painting (思维)

    传送门 大体思路就是:枚举所有可能的水平对称线,计算面积更新答案. 所有可能的水平对称线:\(L_i,R_i,{L_i+R_i\over 2}\) 计算面积:将所有可能的水平对称线从小到大排序,然后依 ...

  2. hdu 6704 K-th occurrence(后缀数组+可持久化线段树)

    Problem Description You are given a string S consisting of only lowercase english letters and some q ...

  3. windows10与linux进行ftp遇到550 Failed to change directory及553 Could not creat file

    第一个原因: 没有权限,可以使用带有l参数的ls命令来看文件或者目录的权限 ls -l 解决:给本地用户添加一个可写权限 chmod +w /home/student ##给对应的本地用户添加一个可写 ...

  4. SPF POJ - 1523 割点+并查集

    题意: 问你这个图中哪个点是割点,如果把这个点去掉会有几个子网 代码: 1 //给你几个点,用着几个点形成了一个图.输入边形成的图,问你这个图中有多少个割点.每一个割点去掉后会形成几个强连通分量 2 ...

  5. iOS网页调试

    iOS上安装Chrome 打开Chrome://inspect,选择开始收集日志 新选项卡中访问目标站点 切换回日志收集页面,即可看到日志信息 https://blog.chromium.org/20 ...

  6. kubernetes跑jenkins动态slave

    使用jenkins动态slave的优势: 服务高可用,当 Jenkins Master 出现故障时,Kubernetes 会自动创建一个新的 Jenkins Master 容器,并且将 Volume ...

  7. codeforces - 978D【思维】

    D. Almost Arithmetic Progression time limit per test 1 second memory limit per test 256 megabytes in ...

  8. 51nod 1073约瑟夫环 递归公式法

    约瑟夫环问题的原来描述为,设有编号为1,2,--,n的n(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,再从他的下一个人起重新报数,报到m时停止报数,报m的出圈,- ...

  9. BellmanFord为什么只需松弛V-1次

    首先s不用松弛,V-=1 然后对于其他的顶点..每次都至少能完全松弛一个顶点.. 为什么呢..因为初始d[s]=0,所以和s相邻接的边都将被松弛完全..无论松弛的顺序 那么对于这个图,无论松弛的顺序都 ...

  10. Electron Security All In One

    Electron Security All In One https://www.electronjs.org/docs/tutorial/security CSP Content-Security- ...