C#基础:ref 参数_c# ref和out参数的区别
wptr33 2025-09-01 15:47 9 浏览
例
在下面,我们定义了 ref 方法的语法。ref 方法具有 retrun 类型,例如 int、float 或 string,以及一个 methodName,它可以是方法的任何合适名称,我们定义了参数 ref 与第一个 ref keword,然后它将具有任何数据类型,例如 int、string 和参数名称,例如 referenceParameter,在下面的语法中,我们定义了使用此 ref 参数的参数主体,并将赋值给此 ref 参数。
returnType methodName(parameters, ref dataType referenceParameter)
{
// Method body
// The method assigns a value to referenceParameter
// ...
}
ref 参数的基本结构
方法签名:
定义接受 ref 关键字的方法时,请在方法签名中的参数类型之前指定 ref 关键字。
参数传递:
调用该方法时,还必须使用 ref 关键字来指示要传递对变量的引用。
使用简单变量的示例一
在下面的示例中,我们有私有类 Program,这个 Program 类有 Main 入口方法。在 Main 入口方法中,我们定义了 SimpleWithRef 方法,该方法没有任何返回类型,并且具有整数类型 x 的 ref 参数。在 SimpleWithRef 方法的主体中,我们为 x 变量分配了一个值。实际上,我们正在用新的值修改现有的值。在 SimpleWithRef 方法之外,我们声明了一个整数类型 x vaiable,并为此 x 变量赋值了 10 值。我们在调用 SimpleWithRef 方法之前打印 x 变量的值。即通话前的 10 点。在此之后,我们调用 SimpleWithRef 函数,该函数必须具有 ref 关键字,然后是变量名称 x。在此之后,我们再次打印 x 的值。现在,您可以监控 x 参数的原始值在调用后是否发生了变化。现在已经变成了 30 岁。
using System;
public class Program
{
public static void Main(string[] args)
{
void Simple(int y)
{
y = 40; //Not Modifies the original 'y' variable
}
void SimpleWithRef(ref int x)
{
x = 30; // Modifies the original 'x' variable
}
int x = 10;
Console.WriteLine("Before call x value is=" + x);
SimpleWithRef(ref x); // Calling the SimpleWithRef function
Console.WriteLine("After call x value is=" + x);
int y = 20;
Console.WriteLine("Before cal y value is=" + y);
Simple(y); // Calling the Simple function
Console.WriteLine("After call y value is=" + y);
}
}
输出
Before call x value is=10
After call x value is=30
Before cal y value is=20
After call y value is=20
带有类的示例 2
using System;
public class Program
{
public static void Main(string[] args)
{
Fruit objFruit = new Fruit(18, "WaterMelon", true);
int fieldPrice = objFruit.GetFruitPrice; // Accessing a field fruit price
string fieldName = objFruit.GetFruitName; // Accessing a field fruit name
bool fieldIsInMarket = objFruit.GetFruitIsInMarket; // Accessing a field fruit is in market
Console.WriteLine("Fruit result before using ref keword");
Console.WriteLine("Fruit name: " + fieldName + " Fruit price:" + fieldPrice + " Is available in summer :" + fieldIsInMarket);
objFruit.SetFruit(ref objFruit.FruitPrice, ref objFruit.FruitName, ref objFruit.FruitIsAvialableInSummer);
Console.WriteLine("Fruit result after using ref keword without class");
Console.WriteLine("Fruit name: " + objFruit.FruitName + " Fruit price:" + objFruit.FruitPrice + " Is available in summer :" + objFruit.FruitIsAvialableInSummer);
objFruit.SetNewFruit(ref objFruit);
Console.WriteLine("Fruit result after using ref keword with class");
Console.WriteLine("Fruit name: " + objFruit.FruitName + " Fruit price:" + objFruit.FruitPrice + " Is available in summer :" + objFruit.FruitIsAvialableInSummer);
}
}
public class Fruit
{
// Fields (attributes or variables)
public int FruitPrice;
public string FruitName;
public bool FruitIsAvialableInSummer;
// Constructor
public Fruit(int price, string name, bool isAvailable)
{
FruitPrice = price;
FruitName = name;
FruitIsAvialableInSummer = isAvailable;
}
// Methods (functions or behaviors)
public int GetFruitPrice
{
return FruitPrice;
}
public string GetFruitName
{
return FruitName;
}
public bool GetFruitIsInMarket
{
return FruitIsAvialableInSummer;
}
public void SetFruit(ref int FruitPrice, ref string FruitName, ref bool FruitIsAvialableInSummer)
{
FruitPrice = 28;
FruitName = "Orange";
FruitIsAvialableInSummer = false;
}
public void SetNewFruit(ref Fruit fruitChange)
{
fruitChange.FruitPrice = 23;
fruitChange.FruitName = "Mango";
fruitChange.FruitIsAvialableInSummer = true;
}
}
输出
Fruit result before using ref keword
Fruit name: WaterMelon Fruit price:18 Is available in summer :True
Fruit result after using ref keword without class
Fruit name: Orange Fruit price:28 Is available in summer :False
Fruit result after using ref keword with class
Fruit name: Mamgo Fruit price:23 Is available in summer :True
使用 struct 的示例 3
using System;
public class Program
{
public static void Main(string[] args)
{
Point p1 = new Point(10, 20);
Point p2 = p1; // Copying p1 to p2
p2.SetValuePoint(ref p2);
Console.WriteLine($"p1: {p1}"); // Output: p1: (10, 20)
Console.WriteLine($"p2: {p2}"); // Output: p2: (30, 20)
}
}
public struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
public override string ToString
{
return $"({X}, {Y})";
}
public void SetValuePoint(ref Point p2)
{
p2.X = 30; // Modifying p2
}
}
输出
p1: (10, 20)
p2: (30, 20)
为什么我们使用 ref 关键字?
1. 修改原始值:
当需要修改原始变量时,将方法的声明范围排除在外。
2. 复制大型数据结构
当您需要将大型数据结构从一个位置传递到另一个位置时。您可以使用 ref kayword 传递这些大数据,以避免在更改所需位置后复制数据。
3. 恢复多个值
当您需要返回多个值时,您可以使用 ref 关键字。
我们可以通过 ref 关键字将变量或类变量的原始值更改为另一个类,因为 ref 关键字构成了变量或类引用类型。
如果你喜欢我的文章,请给我一个赞!谢谢
相关推荐
- 如何在Linux系统中安装Docker?_如何在Linux系统中安装软件
-
在这篇博客中,我将引导您通过简单的步骤完成安装Docker的过程,安装docker只是小菜一碟,你只需要运行几条命令就大功告成了!...
- 我用Docker安装FastDFS,再也不用头疼那些错误提示了
-
在这里插入图片描述FastDFS的安装我们还是通过Docker来安装实现吧,直接在Linux上还装还是比较繁琐的,但就学习而言Docker安装还是非常高效的。Docker环境请自行安装哦,不清楚的...
- 01背包问题的js解决方式_背包算法java
-
如果你有兴趣看这个相信你已经对背包问题有所了解,所以关于背包问题的描述,我就不写了。...
- 净现值函数_净现值函数名词解释
-
此页面特定于Office2010的VisualBasicforApplications(VBA)语言参考。返回一个Double,指定基于一系列定期现金流(付款和收款)和贴现率的投资的...
- Excel 数据分组双利器:GROUPBY 与 FREQUENCY 函数详解
-
这是一篇关于Excel中GROUPBY和FREQUENCY函数的详细教学教程。这两个函数都用于数据分组统计,但它们的应用场景、功能和用法有显著不同。第一部分:强大的新函数——GROUP...
- 熬夜7天,我总结了JavaScript与ES的25个知识点
-
前言说起JavaScript,大家都知道是一门脚本语言。那么ES是什么鬼呢?ES全称ECMAScript,是JavaScript语言的国际标准。最近,我总结了25条JavaScript的基础特性相关...
- 傻傻分不清楚的点积与矩阵乘法 Part3
-
作者:MinkyungKang...
- Python中的数据导入与查询_python如何导入数据文件
-
适用场景...
- 10个JavaScript一行代码,解决90%的开发难题
-
在JavaScript开发过程中,我们经常会遇到一些看似复杂但实际上可以通过简洁的代码解决的问题。下面分享10个JavaScript一行代码技巧,解决日常开发中的常见难题。...
- 提高 PHP 代码质量的 36 计_php代码调试心得
-
1.不要使用相对路径常常会看到:require_once('../../lib/some_class.php');该方法有很多缺点:...
- PHP替换字符串关键词长词优先函数
-
如何实现phpstr_replace替换关键词,如何控制长词优先,也不难,我就写了个这样的函数。functionmyreplace($string,$replaces){...
- PHP 中数组是如何灵活支持多数据类型的?
-
hello,大家好,我是张张,「架构精进之路」公号作者。...
- 3分钟短文 | PHP判断null,别再 == 了,你真控制不住
-
引言PHP程序中很多地方会用到判断是否为空,比如字符串为空,数组为空,对象为空,或者其他数据类型为默认空值。今天我们说一下判断null的两种方法的区别。一般可以使用is_null函数,判断变...
- C#基础:ref 参数_c# ref和out参数的区别
-
例在下面,我们定义了ref方法的语法。ref方法具有retrun类型,例如int、float或string,以及一个methodName,它可以是方法的任何合适名称,我们定义了参数...
- 「C#.NET 拾遗补漏」05:操作符的几个骚操作
-
阅读本文大概需要1分钟。大家好,这是极客精神【C#.NET拾遗补漏】专辑的第5篇文章,今天要讲的内容是操作符。操作符的英文是Operator,在数值计算中习惯性的被叫作运算符,所以在中文的...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
程序员的开源月刊《HelloGitHub》第 71 期
-
Java面试必考问题:什么是乐观锁与悲观锁
-
假如有100W个用户抢一张票,除了负载均衡办法,怎么支持高并发?
-
详细介绍一下Redis的Watch机制,可以利用Watch机制来做什么?
-
如何将AI助手接入微信(打开ai手机助手)
-
redission YYDS spring boot redission 使用
-
SparkSQL——DataFrame的创建与使用
-
Distinct vs Group By:MySQL 查询性能到底谁更强?
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)