📌  相关文章
📜  获取表示元组值的字符串<T1,T2,T3>C#中的实例

📅  最后修改于: 2021-05-29 18:27:08             🧑  作者: Mango

元组是一种数据结构,它为您提供了表示数据集的最简单方法。您还可以使用ToString方法获得表示元组对象的字符串。此方法返回一个字符串,该字符串将表示Tuple 对象。此方法表示的字符串的格式为(Item1,Item2,Item3),其中Item1,Item2,Item3表示Item1,Item2,Item3属性的值。如果任何属性包含空值,它将表示一个String.Empty

句法:

public override string ToString ();

返回类型:此方法的返回类型为System.String 。因此,它将返回一个表示Tuple 对象的字符串。

范例1:

// C# program to illustrate 
// the use of ToString method
using System;
  
namespace exampleoftuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    { 
  
        // 1-Tuple
        var v1 = Tuple.Create("Rohit");
          
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 1: " + v1.ToString());
  
        // 2-Tuple
        var v2 = Tuple.Create("Sheema", "Riya");
  
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 2: " + v2.ToString());
  
        // 3-Tuple
        var v3 = Tuple.Create("Rima", "Suman", "Sohan");
  
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 3: " + v3.ToString());
    }
}
}
输出:
Tuple 1: (Rohit)
Tuple 2: (Sheema, Riya)
Tuple 3: (Rima, Suman, Sohan)

范例2:

// C# program to illustrate 
// the use of ToString method
using System;
  
namespace exampleoftuple {
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        // Nested Tuples
        var T1 = Tuple.Create("Sumit", Tuple.Create("Bongo",
                                          "Bella", "Binu"));
  
        var T2 = Tuple.Create("Boond", "Cinki", "Chimmy",
                         Tuple.Create("Karan", "Micky"));
  
        var T3 = Tuple.Create(34.9, 78.7, 
          Tuple.Create(12.2, 34.5, 5.6, .78));
  
        var T4 = Tuple.Create(2, 4, 6, 8, 5,
           Tuple.Create(10, 20, 30, 40, 50));
  
        // Get the value of Nested Tuples
        // With the help of ToString method
        Console.WriteLine("NTuple 1: {0}", T1.ToString());
        Console.WriteLine("NTuple 2: {0}", T2.ToString());
        Console.WriteLine("NTuple 3: {0}", T3.ToString());
        Console.WriteLine("NTuple 4: {0}", T4.ToString());
    }
}
}
输出:
NTuple 1: (Sumit, (Bongo, Bella, Binu))
NTuple 2: (Boond, Cinki, Chimmy, (Karan, Micky))
NTuple 3: (34.9, 78.7, (12.2, 34.5, 5.6, 0.78))
NTuple 4: (2, 4, 6, 8, 5, (10, 20, 30, 40, 50))