C#基础:ref 参数_c# ref和out参数的区别
wptr33 2025-09-01 15:47 21 浏览
例
在下面,我们定义了 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 关键字构成了变量或类引用类型。
如果你喜欢我的文章,请给我一个赞!谢谢
相关推荐
- [常用工具] git基础学习笔记_git工具有哪些
-
添加推送信息,-m=messagegitcommit-m“添加注释”查看状态...
- centos7安装部署gitlab_centos7安装git服务器
-
一、Gitlab介1.1gitlab信息GitLab是利用RubyonRails一个开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。...
- 太高效了!玩了这么久的Linux,居然不知道这7个终端快捷键
-
作为Linux用户,大家肯定在Linux终端下敲过无数的命令。有的命令很短,比如:ls、cd、pwd之类,这种命令大家毫无压力。但是,有些命令就比较长了,比如:...
- 提高开发速度还能保证质量的10个小窍门
-
养成坏习惯真是分分钟的事儿,而养成好习惯却很难。我发现,把那些对我有用的习惯写下来,能让我坚持住已经花心思养成的好习惯。...
- 版本管理最好用的工具,你懂多少?
-
版本控制(Revisioncontrol)是一种在开发的过程中用于管理我们对文件、目录或工程等内容的修改历史,方便查看更改历史记录,备份以便恢复以前的版本的软件工程技术。...
- Git回退到某个版本_git回退到某个版本详细步骤
-
在开发过程,有时会遇到合并代码或者合并主分支代码导致自己分支代码冲突等问题,这时我们需要回退到某个commit_id版本1,查看所有历史版本,获取git的某个历史版本id...
- Kubernetes + Jenkins + Harbor 全景实战手册
-
Kubernetes+Jenkins+Harbor全景实战手册在现代企业级DevOps体系中,Kubernetes(K8s)、Jenkins和Harbor组成的CI/CD流水...
- git常用命令整理_git常见命令
-
一、Git仓库完整迁移完整迁移,就是指,不仅将所有代码移植到新的仓库,而且要保留所有的commit记录1.随便找个文件夹,从原地址克隆一份裸版本库...
- 第三章:Git分支管理(多人协作基础)
-
3.1分支基本概念分支是Git最强大的功能之一,它允许你在主线之外创建独立的开发线路,互不干扰。理解分支的工作原理是掌握Git的关键。核心概念:HEAD:指向当前分支的指针...
- 云效Codeup怎么创建分支并进行分支管理
-
云效Codeup怎么创建分支并进行分支管理,分支是为了将修改记录分叉备份保存,不受其他分支的影响,所以在同一个代码库里可以同时进行多个修改。创建仓库时,会自动创建Master分支作为默认分支,后续...
- git 如何删除本地和远程分支?_git怎么删除远程仓库
-
Git分支对于开发人员来说是一项强大的功能,但要维护干净的存储库,就需要知道如何删除过时的分支。本指南涵盖了您需要了解的有关本地和远程删除Git分支的所有信息。了解Git分支...
- git 实现一份代码push到两个git地址上
-
一直以来想把自己的博客代码托管到github和coding上想一次更改一次push两个地址一起更新今天有空查资料实践了下本博客的github地址coding的git地址如果是Gi...
- git操作:cherry-pick和rebase_git cherry-pick bad object
-
在编码中经常涉及到分支之间的代码同步问题,那就需要cherry-pick和rebase命令问题:如何将某个分支的多个commit合并到另一个分支,并在另一个分支只保留一个commit记录解答:假设有两...
- 模型文件硬塞进 Git,GitHub 直接打回原形:使用Git-LFS管理大文件
-
前言最近接手了一个计算机视觉项目代码是屎山就不说了,反正我也不看代码主要就是构建一下docker镜像,测试一下部署的兼容性这本来不难但是,国内服务器的网络环境实在是恶劣,需要配置各种镜像(dock...
- 防弹少年团田柾国《Euphoria》2周年 获世界实时趋势榜1位 恭喜呀
-
当天韩国时间凌晨3时左右,该曲在Twitter上以“2YearsWithEuphoria”的HashTag登上了世界趋势1位。在韩国推特实时趋势中,从上午开始到现在“Euphoria2岁”的Has...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
程序员的开源月刊《HelloGitHub》第 71 期
-
详细介绍一下Redis的Watch机制,可以利用Watch机制来做什么?
-
假如有100W个用户抢一张票,除了负载均衡办法,怎么支持高并发?
-
Java面试必考问题:什么是乐观锁与悲观锁
-
如何将AI助手接入微信(打开ai手机助手)
-
SparkSQL——DataFrame的创建与使用
-
redission YYDS spring boot redission 使用
-
一文带你了解Redis与Memcached? redis与memcached的区别
-
如何利用Redis进行事务处理呢? 如何利用redis进行事务处理呢英文
-
- 最近发表
- 标签列表
-
- 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)