当前位置:网站首页>C #, introductory tutorial -- a little knowledge about function parameter ref and source program
C #, introductory tutorial -- a little knowledge about function parameter ref and source program
2022-06-22 19:35:00 【Deep confusion】

One 、 The story of opening a hotel room
function (Function or Method or ...) It's the main body of the program , Pillar .
A function often used : Exchange data between two variables .
By default , That's how it's written :
int a = 10;
int b = 20;
int tmp = a;
a = b;
b = tmp;
If every time I meet , That's what they say , Is it a little trouble ? That is .
Remember two basic principles :
(1) As long as it's a repetition , Just write a function !
(2) As long as it is a relatively long thing ( Code ) It is divided into several functions !
thus , Of course, you need to write a function to complete this repeated task .
that , Is that what it says ?
...
public static class Globals
{
public void swap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
}
...
int a = 10;
int b = 20;
Globals.swap(a, b);
Of course , As you expected or More than you expected , This code does not implement a,b Data exchange function .
The end result is a = 10;b = 20;!!!!!
analogy : You and your friends , Changed rooms , But there was no room key exchange ! I have to go back to my room to sleep at night .
Exchange function swap It should be :
public static class Globals
{
public void swap(ref int a,ref int b)
{
int tmp = a;
a = b;
b = tmp;
}
}
The difference lies in ref !!!!!
ref = reference , quote (C/C++ The pointer inside !).
usage :
int a = 10;
int b = 20;
Globals.swap(ref a, ref b);
analogy : You and your friends , Exchanged room keys , It doesn't matter which room you are in now ! Go to another room to sleep at night !

Two 、 Passengers' choice
Knock on the blackboard ! Key points : If the parameter is passed in , Modifying data , And new data needs to be sent out ( The back should be sprinkled !), Must be used ref.

C# There are many types of data , Is it necessary to ref Well ? No !
note :
(1)int,double,float,decimal,char,byte,string wait , If Pass in 、 Efferent , You need to ref.
public void func(ref string a, ref string b)
{
// modify a, b The data of ;
}(2) Array 、 list 、 Pile up 、 Stack and other data sets , Unwanted .
array Array
public void func(double[] a, double[] b)
{
// modify a, b The data of ;
}
list list
public void func(List<double> a, List<double> b)
{
// modify a, b The data of ;
}
(2)Complex, Matrix And so on, we or our friends define the extension type and class ,struct , There is no need to ref.
public void func(Complex a, Complex b)
{
// modify a, b The data of ;
}A little bit more simple : As long as it is not a basic data type , commonly (!!) No need ref .
Non-basic data types , Is a combination of data , What is exposed to everyone is the room card .
double[] a; It's the type , It's a room card !
a[0] Is the data ; Not a room card !
Let's have a preliminary understanding of .
There is another keyword out , Very very much like !

I'll give you a special introduction later out.
Oh ! also in.
边栏推荐
- 实验4 NoSQL和关系数据库的操作比较
- Service实战:使用Service完成一个下载任务
- 新唐NUC980使用记录:开发环境准备与编译配置基础说明
- Paopao Mart: empty souls need stories
- shell脚本详解(十)——sed编辑器的使用方法
- ABAQUS 使用RSG绘制插件初体验
- Shell script explanation (VII) -- regular expression, sort, uniq, tr
- AUTOCAD——五种标注快捷键
- C#,入门教程——关于函数参数ref的一点知识与源程序
- Iplook, as a member of o-ran alliance, will jointly promote the development of 5g industry
猜你喜欢

机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效

How to choose smart home? Take a look at this shopping guide

一款支持HomeKit的摄像头?智汀 IPC摄像头IC1开箱体验
组合学笔记(五)分配格中的链

20billion vs 5billion, how much is the "dehydration" little red book worth?

Service practice: use service to complete a download task

上半年,这个领域竟出了7家新独角兽,资本争抢入局

shell脚本详解(四)——循环语句之while循环和until循环(附加例题及解析)

《被讨厌的勇气》读后感

5G 短消息解决方案
随机推荐
结构型模式之装饰者模式
有效的括号
什么?HomeKit、米家、Aqara等生态也能通过智汀与天猫精灵生态联动?
贪心之区间问题(4)
Thread pool: reading the source code of threadpoolexcutor
Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)
最长公共子序列
界面开发组件DevExpress ASP.NET Core v21.2 - UI组件增强
Is flush easy to open an account? Is it safe to open a mobile account?
Shell script (V) -- function
如何提高工作效率?苹果电脑效率工具合集
[nfs无法挂载问题] mount.nfs: access denied by server while mounting localhost:/data/dev/mysql
【干货|接口测试必备技能-常见接口协议解析】
常用技术注解
函数的导数与微分的关系
贪心之分配问题(1)
AIOps 智能运维经验分享
3GPP 5g R17 standard is frozen, and redcap as an important feature deserves attention!
数组对象根据id一 一对应填入(将arr1填入arr2中)
jniLibs. Srcdirs = ['LIBS'] what's the use?