網站首頁 編程語言 正文
C# 輸出參數out
什么是輸出參數
方法聲明時,使用out修飾符聲明的形參,成為輸出參數。
輸出參數的特點
1、輸出參數不創(chuàng)建新的儲存位置。
2、輸出參數表示的儲存位置就是實參表示的儲存位置。
3、傳遞給輸出參數的實參在方法調用前不需要強制初始化,在方法內部使用該形參時,需要強制賦值一次。
out參數的使用
使用out參數,可以使方法返回多個返回值。
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參數、ref參數與值參數用法
ref參數是引用,out參數為輸出參數。
out參數修飾符
1、當希望方法返回多個值時,聲明 out 方法非常有用。
2、不必初始化作為 out 參數傳遞的變量。然而,必須在方法返回之前為 out 參數賦值。
3、屬性不是變量,不能作為 out 參數傳遞。
?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參數必須在定義方法內進行賦值
? ? ? ? ? ? ? ? isTrue = true;
? ? ? ? ? ? }
? ? ? ? ? ? catch
? ? ? ? ? ? {
? ? ? ? ? ? ? ? isTrue = false;
? ? ? ? ? ? ? ? result = 0;
? ? ? ? ? ? }
? ? ? ? ? ? return isTrue;
? ? ? ? }
該方法返回類型為bool類型,在返回bool類型的同時也順帶返回了int類型的result變量。即,返回兩種變量類型。
ref參數修飾符
1、必須使用初始化過的變量
2、屬性不是變量,不能作為 ref 參數傳遞。
3、Ref則用在要要被調出使用的方法修改調出使用者的引用的時候。
ref參數在定義的方法內對其進行處理,再將結果返回,定義方法無需多余的返回類型。
?static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? //使用ref參數來交換兩個數字的值
? ? ? ? ? ? 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;
? ? ? ? }
總結
原文鏈接:https://blog.csdn.net/TheWindofFate/article/details/122621673
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-06-02 C語言基于EasyX庫實現有圖形界面鐘表_C 語言
- 2023-02-02 完全卸載nginx以及安裝的超詳細步驟_nginx
- 2022-04-28 C#網絡編程之Socket編程_C#教程
- 2022-10-22 Redisson?主從一致性問題詳解_Redis
- 2022-05-10 怎樣理解單項數據流
- 2022-10-20 react實現動態(tài)表單_React
- 2023-02-14 React?Hydrate原理源碼解析_React
- 2022-12-21 Swift使用enum抹平數組元素差異實例詳解_Swift
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支