網(wǎng)站首頁 編程語言 正文
C# 輸出參數(shù)out
什么是輸出參數(shù)
方法聲明時,使用out修飾符聲明的形參,成為輸出參數(shù)。
輸出參數(shù)的特點
1、輸出參數(shù)不創(chuàng)建新的儲存位置。
2、輸出參數(shù)表示的儲存位置就是實參表示的儲存位置。
3、傳遞給輸出參數(shù)的實參在方法調(diào)用前不需要強制初始化,在方法內(nèi)部使用該形參時,需要強制賦值一次。
out參數(shù)的使用
使用out參數(shù),可以使方法返回多個返回值。
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int max;
int min;
int sum;
double avg;
int[] arr = GetMaxMinSumAvg(numbers, out max, out min, out sum, out avg);
Console.WriteLine(max);
Console.WriteLine(min);
Console.WriteLine(sum);
Console.WriteLine(avg);
Console.WriteLine(arr.Length);
Console.ReadKey();
}
public static int[] GetMaxMinSumAvg(int[] nums, out int max, out int min, out int sum, out double avg)
{
int[] res = new int[4];
max = nums.Max();
min = nums.Min();
sum = nums.Sum();
avg = nums.Average();
return res;
}
C#中out參數(shù)、ref參數(shù)與值參數(shù)用法
ref參數(shù)是引用,out參數(shù)為輸出參數(shù)。
out參數(shù)修飾符
1、當(dāng)希望方法返回多個值時,聲明 out 方法非常有用。
2、不必初始化作為 out 參數(shù)傳遞的變量。然而,必須在方法返回之前為 out 參數(shù)賦值。
3、屬性不是變量,不能作為 out 參數(shù)傳遞。
?static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? string s = "123";
? ? ? ? ? ? int result;
? ? ? ? ? ? bool b = MyTest(s,out result);
? ? ? ? }
? ? ? ? public static bool MyTest(string s, out int result)
? ? ? ? {
? ? ? ? ? ? bool isTrue;
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? result = Convert.ToInt32(s);//使用out參數(shù)必須在定義方法內(nèi)進行賦值
? ? ? ? ? ? ? ? isTrue = true;
? ? ? ? ? ? }
? ? ? ? ? ? catch
? ? ? ? ? ? {
? ? ? ? ? ? ? ? isTrue = false;
? ? ? ? ? ? ? ? result = 0;
? ? ? ? ? ? }
? ? ? ? ? ? return isTrue;
? ? ? ? }
該方法返回類型為bool類型,在返回bool類型的同時也順帶返回了int類型的result變量。即,返回兩種變量類型。
ref參數(shù)修飾符
1、必須使用初始化過的變量
2、屬性不是變量,不能作為 ref 參數(shù)傳遞。
3、Ref則用在要要被調(diào)出使用的方法修改調(diào)出使用者的引用的時候。
ref參數(shù)在定義的方法內(nèi)對其進行處理,再將結(jié)果返回,定義方法無需多余的返回類型。
?static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? //使用ref參數(shù)來交換兩個數(shù)字的值
? ? ? ? ? ? int a = 1;
? ? ? ? ? ? int b = 2;
? ? ? ? ? ? Change(ref a, ref b);
? ? ? ? ? ? Console.WriteLine("a{0},b{1}",a,b);
? ? ? ? ? ? Console.ReadKey();
? ? ? ? }
? ? ? ? public static void Change(ref int a, ref int b)
? ? ? ? {
? ? ? ? ? ? int temp;
? ? ? ? ? ? temp = a;
? ? ? ? ? ? a = b;
? ? ? ? ? ? b = temp;
? ? ? ? }
總結(jié)
原文鏈接:https://blog.csdn.net/TheWindofFate/article/details/122621673
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2023-11-26 StringBuffer 和 StringBuilder
- 2022-07-04 Android實現(xiàn)顏色漸變動畫效果_Android
- 2022-06-01 配置ABP框架使用對象映射_實用技巧
- 2022-10-17 Python?pywin32實現(xiàn)word與Excel的處理_python
- 2023-01-13 分布式緩存Redis與Memcached的優(yōu)缺點區(qū)別比較_數(shù)據(jù)庫其它
- 2022-05-01 oracle刪除超過N天數(shù)據(jù)腳本的方法_oracle
- 2022-09-27 Kotlin示例講解標(biāo)準函數(shù)with與run和apply的使用_Android
- 2022-08-31 docker(alpine+golang)?中?hosts?不生效問題解決方法_docker
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支