C# ref and out的使用小结
作者:LeeMacrofeng 发布时间:2021-12-19 22:35:40
相同点:
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;
}
}
}
代码里报错 “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) { }
}
}
上面代码会报错“ '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 参数,而另一个方法不采用这两个参数,则可以进行重载。
来源:https://www.cnblogs.com/LeeMacrofeng/p/14469359.html
猜你喜欢
- 今天写程序的时候用到了附加属性,我是用VS内置的propa的代码段来实现的,代码如下:class Attach {
- 在平时的开发中,我们会经常遇到这样一个需求,要在页面通过一个『导出』按钮把查询出的数据导出到 Excel 表格中。本文即为实现上述需求的一个
- 前言本文主要给大家介绍了关于Kotlin如何开发Android应用的相关内容,关于kotlin我不过多的介绍了,下面直奔主题。第一步:为An
- 我们在平常项目开发中,经常会用到周期性定时任务,这个时候使用定时任务就能很方便的实现。在SpringBoot中用得最多的就是Schedule
- Servlet1 Servlet 简介Servlet就是Sun 公司开发动态web的一门技术Sun在这些API中提供一个接口叫做:Servl
- 问题介绍:用二维数组表示一个迷宫,设置迷宫起点和终点,输出迷宫中的一条通路实现思路:二维数组表示迷宫:0表示路且未走过、1表示墙、2表示通路
- 本文实例总结了C#中split用法。分享给大家供大家参考,具体如下:以下是我转载的两个不同的人的,方便大家及自己查阅string s=&qu
- 本文实例讲述了C#基于COM方式读取Excel表格的方法。分享给大家供大家参考,具体如下:using System;using System
- 前言Future的问题写多线程程序的时候,可以使用Future从一个异步线程中拿到结果,但是如果使用过程中会发现一些问题:如果想要对Futu
- 一、遇到一个问题1、读取CSV文件package com.guor.demo.charset;import java.io.Buffered
- 步骤:1、创建一个项目,该项目主要用来设计用户控件。2、创建一个用户控件窗体,用来设计用户控件。3、向用户控件窗体中添加一个按钮(butto
- 本文章牵涉到的技术点比较多:spring Data JPA、Redis、Spring MVC,Spirng Cache,所以在看这篇文章的时
- java 多线程死锁 相信有过多线程编程经验的朋友,都吃过死锁的苦。除非你不使用多线程,否则死锁的可能性会一直存在。为什么会出现
- 代码如下:public class TempTest { public static void main(String[] args) {
- 常用的类:@ConditionalOnProperty(name = "use.redis.session.store"
- 什么是 Spring Boot 插件?Spring Boot 插件是一种扩展机制,它提供了一种简单的方式来扩展 Spring Boot 的功
- 今天在做项目的时候突然遇到一个问题:启动服务器的时候spring没报错,可是当我访问某个页面的时候spring报Request bean i
- 一、reservedcodecachesize参数介绍该参数是JvM虚拟机调优中调整内存大小的一个设置参数,值得大小设置直接影响到Code
- 一:问题引入前面讲到用户支付完成之后微信支付服务器会发送回调通知给商户,商户要能够正常处理这个回调通知并返回正确的状态码给微信支付后台服务器
- 前言相信大家在使用spring的项目中,前台传递参数到后台是经常遇到的事, 我们必须熟练掌握一些常用的参数传递方式和注解的使用,本文将给大家