| 方案 | 語法 | 適用場景 | 優點 | 缺點 |
|---|---|---|---|---|
| 1. Tuple 元組 (C# 7+) | `(int sum, int count) Calc()` | 2~7 個相關值、臨時組合 | 語法糖、解構賦值、無額外類型 | 字段名僅編譯期有效、跨程序集需注意 |
| 2. 自定義類/結構體 | `class Result { public int A; public int B; }` | 語義明確、復用、需驗證/方法 | 強類型、可擴展、文檔友好 | 樣板代碼多、堆分配(class) |
| 3. 匿名類型 | `return new { X = 1, Y = 2 };` | 內部邏輯、LINQ 投影 | 極簡潔 | 編譯期類型、不可跨方法邊界返回 |
| 4. 引用類型參數傳遞 | `void Calc(List<int> buffer)` | 大數據填充、避免拷貝 | 零分配、可變長 | 副作用隱蔽、需約定調用者初始化 |
| 5. 模式匹配 + out var (C# 7+) | `if (TryParse(s, out var n))` | 嘗試操作、失敗不拋異常 | 語義清晰、配合模式匹配 | 仍屬 out 變體 |
public (int Sum, int Count, double Avg) Stats(int[] nums)
{
int sum = 0, count = 0;
foreach (var n in nums) { sum += n; count++; }
return (sum, count, count > 0 ? (double)sum / count : 0);
}
// 調用
var (s, c, a) = Stats(new[] {1,2,3,4,5});
Console.WriteLine($"{s} {c} {a}"); // 15 5 3
public readonly struct Point3D
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public Point3D(double x, double y, double z) => (X, Y, Z) = (x, y, z);
public void Deconstruct(out double x, out double y, out double z) => (x, y, z) = (X, Y, Z);
}
public Point3D Centroid(Point3D[] pts)
{
double x=0, y=0, z=0;
foreach (var p in pts) { x+=p.X; y+=p.Y; z+=p.Z; }
int n = pts.Length;
return new Point3D(x/n, y/n, z/n);
}
public class OperationResult
{
public bool Success { get; set; }
public string Message { get; set; }
public object Data { get; set; }
public static OperationResult Ok(object data = null) => new() { Success = true, Data = data };
public static OperationResult Fail(string msg) => new() { Success = false, Message = msg };
}
public async Task<(bool Ok, string Token)> LoginAsync(string user, string pwd)
{
var resp = await _http.PostAsJsonAsync("/login", new { user, pwd });
if (resp.IsSuccessStatusCode)
return (true, await resp.Content.ReadAsStringAsync());
return (false, await resp.Content.ReadAsStringAsync());
}
C# 7+ 後,**Tuple 元組是多返回值的首選**:語法原生、解構優雅、零開銷(值類型)。複雜業務用 **readonly struct**(值語義、不可變、高性能)或 **class**(引用語義、可擴展)。合理選擇,代碼既簡潔又專業。