服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# ref and out的使用小结

C# ref and out的使用小结

2022-11-04 12:03LeeMacrofeng C#

这篇文章主要介绍了C# ref and out的使用小结,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下

相同点:

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

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

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

不同点:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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参数的参数值初始化必须在其方法内进行,否则程序会报错。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
    }
 
  }
}

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

下面的代码是正确的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
    }
 
  }
}

输出结果为:

C# ref and out的使用小结

注意点:

?
1
2
3
4
5
6
7
8
9
10
11
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 参数,则无法重载这两个方法。例如,从编译的角度来看,以下代码中的两个方法是完全相同的,因此将不会编译上面的代码。

?
1
2
3
4
5
6
7
8
9
10
using System;
 
namespace ConsoleApplication1
{
  class Program
  {
    public void SampleMethod(int i) { }
    public void SampleMethod(ref int i) { }
  }
}

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

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

?
1
2
3
4
5
6
7
8
9
10
using System;
 
namespace ConsoleApplication1
{
  class Program
  {
    public void SampleMethod(int i) { }
    public void SampleMethod(ref int i) { }
  }
}

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

以上就是C# ref and out的使用小结的详细内容,更多关于C# ref and out的使用的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/LeeMacrofeng/p/14469359.html

延伸 · 阅读

精彩推荐
  • C#C#数据结构之最小堆的实现方法

    C#数据结构之最小堆的实现方法

    这篇文章主要给大家介绍了关于C#数据结构之最小堆的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    鹅厂程序小哥9272022-11-01
  • C#英语单词state与status的区别

    英语单词state与status的区别

    state倾向于condition,是一种延续性的状态。status常用于描述一个过程中的某阶段(phase),类似于C语言中枚举型变量某一个固定的值,这个值属于一个已知...

    likebeta8542021-12-09
  • C#C#实现TreeView节点拖拽的方法

    C#实现TreeView节点拖拽的方法

    这篇文章主要介绍了C#实现TreeView节点拖拽的方法,涉及C#针对TreeView节点的动态添加及移除技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    99re12572021-10-26
  • C#winform实现限制及解除鼠标移动范围的方法

    winform实现限制及解除鼠标移动范围的方法

    这篇文章主要介绍了winform实现限制及解除鼠标移动范围的方法,涉及C#控制WinForm鼠标事件属性的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    C#教程网9102021-10-28
  • C#OpenXml合并Table单元格代码实例

    OpenXml合并Table单元格代码实例

    在本篇文章里小编给大家整理了关于OpenXml合并Table单元格的相关实例详解,需要的朋友们参考下。...

    dzw1596292022-08-01
  • C#C#实现学员信息管理系统

    C#实现学员信息管理系统

    这篇文章主要为大家详细介绍了C#实现学员信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    zxh...3932022-07-27
  • C#Unity Shader相交算法实现简易防能量盾

    Unity Shader相交算法实现简易防能量盾

    这篇文章主要为大家详细介绍了Unity Shader相交算法实现简易防能量盾,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    ZzEeRO11422022-09-05
  • C#Unity Shader实现动态雾效果

    Unity Shader实现动态雾效果

    这篇文章主要为大家详细介绍了Unity Shader实现动态雾效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    ZzEeRO9602022-09-05