網站首頁 編程語言 正文
引言
根據 C# 語言規范,不可能從一個方法返回多個值。使用 C# 提供的一些其他功能,我們可以將多個值返回給調用者方法。本文概述了一些可用的替代方法來實現這一點。
1.使用ref參數
我們可以使用 ref
關鍵字 通過引用將值返回給調用者。我們可以使用它從一個方法中返回多個值,
如下所示:
using System; public class Example { private static void fun(ref int x, ref int y) { x = 1; y = 2; } public static void Main() { int x = 0; int y = 0; fun(ref x, ref y); Console.WriteLine("x = {0}, y = {1}", x, y); } } /* 輸出: x = 1, y = 2 */
請注意, ref
關鍵字不適用于 Async
和 Iterator
方法。
2.使用out參數修飾符
out
關鍵字導致參數通過引用傳遞。它就像 ref 關鍵字,除了 ref 要求在傳遞變量之前對其進行初始化。
下面的例子演示了使用 out 參數從方法返回多個值。
using System; public class Example { private static void fun(out int x, out int y) { x = 1; y = 2; } public static void Main() { int x = 0; int y = 0; fun(out x, out y); Console.WriteLine("x = {0}, y = {1}", x, y); } } /* 輸出: x = 1, y = 2 */
請注意, out 參數不適用于 Async 和 Iterator 方法。
3. 使用元組類
一個 tuple 是一種數據結構,可讓您輕松地將多個值打包到單個對象中。元組通常用于從方法返回多個值。
下面的示例創建一個 2 元組并從 fun() 方法:
using System; public class Example { private static Tuple<int, int> fun() { return Tuple.Create(1, 2); } public static void Main() { Tuple<int, int> tuple = fun(); Console.WriteLine("x = {0}, y = {1}", tuple.Item1, tuple.Item2); } } /* 輸出: x = 1, y = 2 */
tuple
是一個元組,最多支持7個元素,再多需要嵌套等方法實現。
使用元組定義函數的方法如下:
public static Tuple<string,string> TupleFun() { string[] T = {'hello','world'}; Tuple<string, string> tup = new Tuple<string, string>(T[0], T[1]); return tup; }
元組還支持多種類型的值。
public static Tuple<string,int> TupleFun() { string T = ‘hello'; int q = 6; Tuple<string, int> tup = new Tuple<string, int>(T, q); return tup; }
在調用函數時,使用Item*來調用元組內的元素。
var tuple = TupleFun(); print(tuple.Item1); print(int.Parse(tuple.Item2));
4.使用C#7 ValueTuple
值元組,在 .NET Framework 4.7 中引入,是元組類型,用于在 C# 中提供元組的運行時實現。像元組類一樣,我們可以使用它以更有效的方式從方法中返回多個值。
下面的示例使用類型推斷來解構該方法返回的 2 元組。
using System; public class Example { private static (int, int) fun() { return (1, 2); } public static void Main() { (int x, int y) = fun(); Console.WriteLine("x = {0}, y = {1}", x, y); } } /* 輸出: x = 1, y = 2 */
5. 使用結構或類
在這里,想法是返回一個包含我們想要返回的所有字段的類的實例。以下代碼示例從使用 struct 的方法返回多個值。
using System; public class Example { private struct Pair { public int x; public int y; } private static Pair fun() { return new Pair { x = 1, y = 2 }; } public static void Main() { Pair pair = fun(); Console.WriteLine("x = {0}, y = {1}", pair.x, pair.y); } } /* 輸出: x = 1, y = 2 */
這就是從 C# 中的方法返回多個值的全部內容。
原文鏈接:https://blog.csdn.net/weixin_35770067/article/details/127077358
相關推薦
- 2022-03-10 Android如何獲取APP啟動時間_Android
- 2022-11-05 一篇文章說清楚?go?get?使用私有庫的方法_Golang
- 2022-05-28 Entity?Framework?Core生成數據庫表_實用技巧
- 2022-05-17 Mybatis中報錯:attempted to return null from a method
- 2022-10-21 Go語言使用goroutine及通道實現并發詳解_Golang
- 2023-03-01 GoLang中Strconv庫有哪些常用方法_Golang
- 2023-12-07 com.fasterxml.jackson.databind.ObjectMapper
- 2023-12-10 該方法僅能傳入 lambda 表達式產生的合成類
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支