当前位置:网站首页>C#:in、out、ref關鍵字
C#:in、out、ref關鍵字
2022-07-23 13:51:00 【qq_42987967】
一、in關鍵字
in 關鍵字會導致按引用傳遞參數,但確保未修改參數。 它讓形參成為實參的別名,這必須是變量。從C++角度來說,in關鍵字有點類似於const T&
1.in修飾值類型
1)當in修飾基本數據類型時,我們無法修改基本數據類型的值,只能讀取。
void SetValue(in int i)
{
i = 4;//報錯
}
2) 當修飾struct數據類型的時候也是只能讀取。
public struct ValueType
{
public int i;
public void Set()
{
i = 1;
}
}
public class ReadOnlyTest : MonoBehaviour
{
ValueType valueType = new ValueType();
void Seti2(in ValueType valtype)
{
valtype.i = 4;//報錯
}
}
2.in修飾引用類型
引用類型就比較靈活了,其是可以修改引用類型的成員變量的值的。
public class RefType
{
public int i = 0;
}
public class ReadOnlyTest : MonoBehaviour
{
RefType refType = new RefType();
void Seti1(RefType reftype)
{
reftype.i = 4;//可以正常修改
}
}3.在函數中使用in關鍵字可以算是重載的。
void SetValue(in int i)
{
Debug.Log(i);
}
void SetValue(int i)
{
Debug.Log(i);
}
void Start()
{
SetValue(i);
SetValue(in i);
}該部分參考自:in 參數修飾符 - C# 參考 | Microsoft Docs
二、out關鍵字
out關鍵字跟in很像,基本像是一對。不過對out關鍵字來說其不僅可以讀取,還能被賦值,同時也必須被賦值。
void SetValue(out int i)
{
i = 9;
int k=i;
}
void Start()
{
int i = 0;
SetValue(out i);
Debug.Log(i);//將打印9
}三、ref關鍵字
1.官方的提示
1) 在某些情况下,按引用訪問值可避免潜在的高開銷複制操作,從而提高性能。
2)不要混淆通過引用傳遞的概念與引用類型的概念。 這兩種概念是不同的。 無論方法參數是值類型還是引用類型,均可由 ref 修改。 當通過引用傳遞時,不會對值類型裝箱。
2.ref修飾值類型
ref主要對值類型會有較大的影響,其修飾函數形參與in和out沒有很大區別。
但是比較有趣的是其可以直接修飾變量。這真就跟C++的左值引用一模一樣了。
void Start()
{
int i = 0;
ref int k = ref i;
k = 3;
Debug.Log(i);//i=3
}並且還可以跟readonly關鍵字一起使用
void Start()
{
int i = 0;
ref readonly int k = ref i;
k = 3;//只可讀,不可賦值,報錯
Debug.Log(i);
}3.ref、in、out關鍵字的區別
1)傳遞到 ref 或 in 形參的實參必須先經過初始化,然後才能傳遞。 該要求與 out 形參不同,在傳遞之前,不需要顯式初始化該形參的實參。
边栏推荐
- 同花顺开户风险性大吗,安全吗?
- ROS中引用和输出消息类型
- 常用的鼠标事件和键盘事件
- [record] golang cross platform compilation
- ROS2自学笔记:URDF机器人建模
- 概率沉思錄:2.The quantitative rules
- PHP获取当前时间戳三位毫秒 - 毫秒时间戳
- High school Chinese teaching material examination outline
- 数据库系统原理与应用教程(042)—— MySQL 查询(四):使用通配符构造查询条件
- Chapter II relational database after class exercises
猜你喜欢

了解canvas

QT creator.Pro file adds the corresponding library according to the kit

Establish stm32f103c8t6 project template and STM32 st-link utility burn hex file

2022 summer vacation software innovation laboratory training project practice 1

ES6 - weekly examination questions

Redis常用命令
![[play with FPGA in simple terms to learn 10 ----- simple testbench design]](/img/3a/6d2f4f4e35a08803383def9c665df9.png)
[play with FPGA in simple terms to learn 10 ----- simple testbench design]

IP地址分类及范围

建立STM32F103C8T6工程模板和STM32 ST-LINK Utilit烧录hex文件

Backtracking method to solve the eight queens problem
随机推荐
【STM32】串口通信基础知识
[play with FPGA in simple terms to learn 10 ----- simple testbench design]
Database view detailed exploration
LeetCode_51_N皇后
How to deal with the new development mode when doing testing?
腾讯MMKV的原理与实现
数据库系统原理与应用教程(045)—— MySQL 查询(七):聚合函数
Research on hardware architecture of Ti single chip millimeter wave radar xwr1642
Backtracking method to solve the eight queens problem
Redis常用命令
LeetCode_2342_数位和相等数对的最大和
【深入浅出玩转FPGA学习10------简单的Testbench设计】
LeetCode_46_全排列
Reference and output message types in ROS
智慧食堂数据分析系统
Typora 修改表格宽度
微服务重点
2022 summer vacation software innovation laboratory training project practice 1
Remote editing and debugging with vscode
Light chain dissection / tree chain dissection