如何在 C# 中比較兩個元組?


元組比較出現在 C# 7.3 之後。

在 C# 中使用等號運算子輕鬆比較兩個元組。

假設我們有兩個元組 −

var one = (x: 1, y: 2);
var two = (p: 1, 2: 3, r: 3, s:4);

要比較兩個元組,只需使用 == 運算子 −

if (one == two)
Console.WriteLine("Both the tuples are same (values are same).");

讓我們看看這段程式碼 −

示例

var one = (x: 1, y: 2);
var two = (p: 1, 2: 3, r: 3, s:4);

if (one == two)
Console.WriteLine("Both the tuples are same (values are same).");
lse
Console.WriteLine("Both the tuples values are not same.");

更新於: 2020 年 6 月 21 日

361 次瀏覽

開啟您的 事業

完成課程,獲得認證

開始
廣告