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

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

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

句法:

public override string ToString ();

返回类型:此方法的返回类型为System.String 。所以,它会返回一个表示元组对象的字符串。

范例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());
  
        // 4-Tuple
        var v4 = Tuple.Create("Shilpa", "Susma", 
                              "Sumit", "Rohan");
  
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 4: " + v4.ToString());
    }
}
}
输出:
Tuple 1: (Rohit)
Tuple 2: (Sheema, Riya)
Tuple 3: (Rima, Suman, Sohan)
Tuple 4: (Shilpa, Susma, Sumit, Rohan)

范例2:

// C# program to illustrate the
// use of ToString method
using System;
  
namespace exampleoftuple {
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        // 5-Tuple
        var v5 = Tuple.Create("G", "E", "E", "K", "S");
  
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 5: " + v5.ToString());
  
        // 6-Tuple
        var v6 = Tuple.Create("C", "C#", "C++", 
                     "Python", "Java", "Perl");
  
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 6: " + v6.ToString());
  
        // 7-Tuple
        var v7 = Tuple.Create(12, 45, 67, 78, 87, 97, 87);
  
        // Get the value of Tuple
        // With the help of ToString method
        Console.WriteLine("Tuple 7: " + v7.ToString());
  
        // 8-Tuple
        var v8 = Tuple.Create("G", "E", "E", "K", "S",
                 "F", "O", Tuple.Create("R", "G", "E", 
                                      "E", "K", "S"));
  
        // Get the value of Tuple<
        // With the help of ToString method
        Console.WriteLine("Tuple 8: " + v8.ToString());
    }
}
}
输出:
Tuple 5: (G, E, E, K, S)
Tuple 6: (C, C#, C++, Python, Java, Perl)
Tuple 7: (12, 45, 67, 78, 87, 97, 87)
Tuple 8: (G, E, E, K, S, F, O, (R, G, E, E, K, S))