網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
簡(jiǎn)介:
In:過(guò)程不會(huì)改寫In的內(nèi)容 ,默認(rèn)的傳遞方式,即向函數(shù)內(nèi)部傳送值。
Out和out:傳入的值不會(huì)被過(guò)程所讀取,Out在傳入的時(shí)候,參數(shù)的數(shù)值會(huì)清空,但過(guò)程可以寫 。只出不進(jìn)
ref:可以把參數(shù)的數(shù)值傳遞進(jìn)函數(shù) ,過(guò)程會(huì)讀,會(huì)寫 。有進(jìn)有出。
一、In
In 關(guān)鍵字使參數(shù)按值傳遞。即向函數(shù)內(nèi)部傳送值。
例如:
using System;
class gump
{
public double square(double x)
{
x=x*x;
return x;
}
}
class TestApp
{
public static void Main()
{
gump doit=new gump();
double a=3;
double b=0;
Console.WriteLine(\"Before square->a={0},b={1}\",a,b);//a=3,b=0;
b=doit.square( a);
Console.WriteLine(\"After square->a={0},b={1}\",a,b);//a=3,b=9;
}
}
二、ref
ref?關(guān)鍵字使參數(shù)按引用傳遞。其效果是,當(dāng)控制權(quán)傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)的任何更改都將反映在該變量中。若要使用?ref?參數(shù),則方法定義和調(diào)用方法都必須顯式使用?ref?關(guān)鍵字。
例如:
using System;
class gump
{
public double square(double x)
{
x=x*x;
return x;
}
}
class TestApp
{
public static void Main()
{
gump doit=new gump();
double a=3;
double b=0;
Console.WriteLine(\"Before square->a={0},b={1}\",a,b);//a=3,b=0;
b=doit.square( ref a);
Console.WriteLine(\"After square->a={0},b={1}\",a,b);//a=9,b=9;
}
}
傳遞到?ref?參數(shù)的參數(shù)必須最先初始化。這與?out?不同,后者的參數(shù)在傳遞之前不需要顯式初始化。
三、out
out?關(guān)鍵字會(huì)導(dǎo)致參數(shù)通過(guò)引用來(lái)傳遞。這與?ref?關(guān)鍵字類似,不同之處在于?ref?要求變量必須在傳遞之前進(jìn)行初始化。若要使用?out?參數(shù),方法定義和調(diào)用方法都必須顯式使用out?關(guān)鍵字。
using System;
class gump
{
public void math_routines(double x,out double half,out double squared,out double cubed)
//可以是:public void math_routines(ref double x,out double half,out double squared,out double cubed)
//但是,不可以這樣:public void math_routines(out double x,out double half,out double squared,
//out double cubed),對(duì)本例來(lái)說(shuō),因?yàn)檩敵龅闹狄縳賦值,所以x不能再為輸出值
{
half=x/2;
squared=x*x;
cubed=x*x*x;
}
}
class TestApp
{
public static void Main()
{
gump doit=new gump();
double x1=600;
double half1=0;
double squared1=0;
double cubed1=0;
[Page]
/*
double x1=600;
double half1;
double squared1;
double cubed1;
*/
Console.WriteLine(\"Before method->x1={0}\",x1);
Console.WriteLine(\"half1={0}\",half1); Console.WriteLine(\"squared1={0}\",squared1);
Console.WriteLine(\"cubed1={0}\",cubed1);
doit.math_rountines(x1,out half1,out squared1,out cubed1);
//此時(shí)的Out修飾的參數(shù)值均已經(jīng)發(fā)生改變。
Console.WriteLine(\"After method->x1={0}\",x1);
Console.WriteLine(\"half1={0}\",half1);
Console.WriteLine(\"squared1={0}\",squared1);
Console.WriteLine(\"cubed1={0}\",cubed1);
}
}
盡管作為?out?參數(shù)傳遞的變量不必在傳遞之前進(jìn)行初始化,但需要調(diào)用方法以便在方法返回之前賦值。
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
四、區(qū)別:
1.ref和out的區(qū)別在C# 中,既可以通過(guò)值也可以通過(guò)引用傳遞參數(shù)。通過(guò)引用傳遞參數(shù)允許函數(shù)成員更改參數(shù)的值,并保持該更改。若要通過(guò)引用傳遞參數(shù), 可使用ref或out關(guān)鍵字。ref和out這兩個(gè)關(guān)鍵字都能夠提供相似的功效,其作用也很像C中的指針變量。
2.使用ref型參數(shù)時(shí),傳入的參數(shù)必須先被初始化。對(duì)out而言,必須在方法中對(duì)其完成初始化。
3.使用ref和out時(shí),在方法的參數(shù)和執(zhí)行方法時(shí),都要加Ref或Out關(guān)鍵字。以滿足匹配。
4.out適合用在需要retrun多個(gè)返回值的地方,而ref則用在需要被調(diào)用的方法修改調(diào)用者的引用的時(shí)候。
5.方法參數(shù)上的 out 方法參數(shù)關(guān)鍵字使方法引用傳遞到方法的同一個(gè)變量。當(dāng)控制傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)所做的任何更改都將反映在該變量中。
原文鏈接:https://www.cnblogs.com/wml-it/p/14776395.html
相關(guān)推薦
- 2023-01-20 Python輸入圓半徑,計(jì)算圓周長(zhǎng)和面積的實(shí)現(xiàn)方式_python
- 2022-12-09 Python?keras.metrics源代碼分析_python
- 2022-07-15 Android自定義view繪制表格的方法_Android
- 2023-01-01 Pycharm沒(méi)有報(bào)錯(cuò)提示(誤觸ignore)的完美解決方案_python
- 2022-03-20 android自定義對(duì)話框?qū)嵗a_Android
- 2022-08-17 Win2008系統(tǒng)搭建DHCP服務(wù)器_win服務(wù)器
- 2022-08-10 pandas.DataFrame.from_dict直接從字典構(gòu)建DataFrame的方法_pyth
- 2022-12-13 Python按天實(shí)現(xiàn)生成時(shí)間范圍序列的方法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支