当前位置:网站首页>C#,入门教程——关于函数参数ref的一点知识与源程序
C#,入门教程——关于函数参数ref的一点知识与源程序
2022-06-22 17:56:00 【深度混淆】

一、宾馆开房的故事
函数(Function or Method or ...)是程序的主体,栋梁。
经常用到的一个功能:交换两个变量的数据。
默认情况下,这么写:
int a = 10;
int b = 20;
int tmp = a;
a = b;
b = tmp;
如果每次遇到,都这么写,是不是有点费事呢?那是。
记住二条基本原则:
(1)只要是重复的事情,就写作函数!
(2)只要是比较长的事情(代码)就分成几个函数!
因而,当然要写个函数来完成这个重复的任务。
那,是不是这么写?
...
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);
当然,正如你所预料 或 超乎你的预料,这段代码没实现a,b数据交换功能。
最后结果是 a = 10;b = 20;!!!!!
比喻:你和朋友,换了房间,但是没有交换房间钥匙!晚上还得回自己房间睡觉。
交换函数 swap 应该这么写:
public static class Globals
{
public void swap(ref int a,ref int b)
{
int tmp = a;
a = b;
b = tmp;
}
}
区别在于 ref !!!!!
ref = reference ,引用(C/C++里面的指针!)。
用法:
int a = 10;
int b = 20;
Globals.swap(ref a, ref b);
比喻:你和朋友,交换了房间钥匙,现在在哪个房间不重要了!晚上去另外房间睡觉!

二、旅客的选择
敲黑板啦!关键点: 如果参数传入后,修改数据,并且需要传出新的数据(后面要用撒!),则必须使用 ref。

C# 的数据类型很多,是不是都需要 ref 呢?不是的啊!
笔记:
(1)int,double,float,decimal,char,byte,string等等,如果 传入、传出,则需要 ref。
public void func(ref string a, ref string b)
{
// 修改 a, b 的数据;
}(2)数组、列表、堆、栈等数据集合,不需要。
array 数组
public void func(double[] a, double[] b)
{
// 修改 a, b 的数据;
}
list 列表
public void func(List<double> a, List<double> b)
{
// 修改 a, b 的数据;
}
(2)Complex, Matrix 等等咱们或朋友定义的扩展类型与 class ,struct ,就不需要 ref。
public void func(Complex a, Complex b)
{
// 修改 a, b 的数据;
}简单一点:只要不是基本的数据类型,一般(!!)都不需要 ref 。
非基本数据类型,是组合数据,本身暴露给大家的就是房卡而已。
double[] a;是类型,是房卡!
a[0] 是数据;不是房卡!
大家先初步了解一下。
另外还有一个关键字 out ,非常非常像!

以后再专门介绍 out。
哦!还有 in。
边栏推荐
- Notes on Combinatorics (V) chains in distributive lattice
- 一些技术想法:
- Redis usage scenario sharing (project practice)
- org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
- 20billion vs 5billion, how much is the "dehydration" little red book worth?
- shell脚本(五)——函数
- SystemVerilog (12) - $unit declaration space
- China's two meteorological "new stars" data products are shared with global users
- 程序员工具大全【持续更新】
- Iplook 5gc successfully connected with CICA international CHF (billing function)
猜你喜欢
Golang implements reliable delay queue based on redis

Vs Code suddenly fails to jump

Exness sorted out three problems to be solved in Musk's acquisition of Twitter

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

Iplook 5gc successfully connected with CICA international CHF (billing function)

程序员工具大全【持续更新】

《被讨厌的勇气》读后感

函数的导数与微分的关系

What happened to this page when sqlserver was saving

20billion vs 5billion, how much is the "dehydration" little red book worth?
随机推荐
C sqlsugar, hisql, FreeSQL ORM framework omni-directional performance test comparison sqlserver
一些技术想法:
Message Oriented Middleware (I) MQ explanation and comparison of four MQS
shell脚本(五)——函数
JSP connection MySQL total error
Active Directory用户登录报告
wpa_supplicant的状态机迁移
Implementing Domain Driven Design - using ABP framework - solution overview
Paopao Mart: empty souls need stories
2022 t elevator repair recurrent training question bank and answers
同花顺好用么?手机开户安全么?
链表4- 21 合并两个有序链表
DBMS in Oracle_ output. put_ Example of line usage
Which securities company is better to open an account when making an appointment to play new bonds? It is safer to open an account
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
运维、监控、AIOps的几个重要观点
Niuke network: minimum coverage substring
如何在 FlowUs和Notion 等笔记软件中进行任务管理?
預訓練語言模型,bert,RoFormer-Sim又稱SimBERTv2
Modèle de langage de pré - formation, Bert, roformer Sim aussi connu sous le nom de simbertv2