当前位置:网站首页>C #: in, out, ref keywords
C #: in, out, ref keywords
2022-07-23 13:52:00 【qq_ forty-two million nine hundred and eighty-seven thousand ni】
One 、in keyword
in Keywords cause parameters to be passed by reference , But make sure that the parameters are not modified . It makes a formal parameter an alias for an argument , This must be a variable . from C++ perspective ,in Keywords are a bit like const T&
1.in Modifier value type
1) When in When decorating basic data types , We cannot modify the value of basic data type , Can only read .
void SetValue(in int i)
{
i = 4;// Report errors
}
2) When modified struct Data types can only be read .
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;// Report errors
}
}
2.in Decorate reference types
Reference types are more flexible , It can modify the value of the member variable of the reference type .
public class RefType
{
public int i = 0;
}
public class ReadOnlyTest : MonoBehaviour
{
RefType refType = new RefType();
void Seti1(RefType reftype)
{
reftype.i = 4;// It can be modified normally
}
}3. Use... In functions in Keywords can be overloaded .
void SetValue(in int i)
{
Debug.Log(i);
}
void SetValue(int i)
{
Debug.Log(i);
}
void Start()
{
SetValue(i);
SetValue(in i);
}This part refers to :in Parameter modifiers - C# Reference resources | Microsoft Docs
Two 、out keyword
out Key words follow in It's like , Basically like a couple . But for out For keywords, it can not only read , It can also be assigned , along with it Must be assigned .
void SetValue(out int i)
{
i = 9;
int k=i;
}
void Start()
{
int i = 0;
SetValue(out i);
Debug.Log(i);// Will print 9
}3、 ... and 、ref keyword
1. Official tips
1) In some cases , Accessing values by reference avoids potentially costly replication operations , To improve performance .
2) Don't confuse the concept of passing by reference with the concept of reference type . These two concepts are different . Whether the method parameter is a value type or a reference type , Can be by ref modify . When passed by reference , Value types are not boxed .
2.ref Modifier value type
ref It mainly affects the value type , Its modifier function shape participates in and out There's no big difference .
But what is more interesting is that it can directly modify variables . This is really like C++ The lvalue reference of is exactly the same .
void Start()
{
int i = 0;
ref int k = ref i;
k = 3;
Debug.Log(i);//i=3
}And you can also follow readonly Use keywords together
void Start()
{
int i = 0;
ref readonly int k = ref i;
k = 3;// Read only , Non assignable , Report errors
Debug.Log(i);
}3.ref、in、out The difference between keywords
1) Pass on to ref or in Arguments of formal parameters must be initialized first , Then you can pass . This requirement is consistent with out The shape parameters are different , Before delivery , There is no need to explicitly initialize the arguments of this formal parameter .
This part refers to :ref keyword - C# Reference resources | Microsoft Docs
边栏推荐
猜你喜欢

图形管线(一)后处理阶段 alpha测试 模版测试 深度测试 混合

CenterNet目标检测模型及CenterFusion融合目标检测模型

C # make a simple browser
Smart canteen data analysis system

Client does not support authentication protocol requested by server; consider upgrading MySQL client

企业科技展厅制作需要考虑这五点

内存取证之NSSCTF-OtterCTF 2018(复现赛)

China leads the United States in another emerging technology field and stands at the commanding height of scientific and technological innovation

LeetCode_52_N皇后Ⅱ

Color recognition of regions of interest in pictures and videos based on OpenCV
随机推荐
数据库系统原理与应用教程(045)—— MySQL 查询(七):聚合函数
[graphics] ASTC texture compression format
[Muduo] epollplayer event distributor
200 行代码,深入分析动态计算图的原理及实现
内存取证之NSSCTF-OtterCTF 2018(复现赛)
PHP gets the current timestamp three bit MS MS timestamp
Remote editing and debugging with vscode
图像处理7:测试文件
数据库系统原理与应用教程(043)—— MySQL 查询(五):对查询结果排序
欧洲“气荒”对中国有哪些影响?
Warcraft map editor trigger notes
docker redis
数据库系统原理与应用教程(040)—— MySQL 查询(二):设置要查询的列名或表达式
建立STM32F103C8T6工程模板和STM32 ST-LINK Utilit烧录hex文件
Client does not support authentication protocol requested by server; consider upgrading MySQL client
How to deal with the new development mode when doing testing?
QT creator.Pro file adds the corresponding library according to the kit
Matlab: convert PNG pictures into txt files
Special lecture 5 combinatorial mathematics learning experience (long-term update)
同花顺开户风险性大吗,安全吗?