如何在 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-06-21

359 瀏覽量

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告