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

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

ValueTuple是C#7.0中引入的结构,表示值类型Tuple。它允许您存储一个数据集,该数据集包含可能彼此相关或不相关的多个值。您还可以在ToString方法的帮助下获得一个表示ValueTuple对象值的字符串。
此方法返回一个字符串,该字符串将表示ValueTuple 对象的值。此方法表示的字符串的格式为(Item1,Item2,Item3,Item4,Item5,Item6,Item7),其中Item1,Item2,Item3,Item4,Item5,Item6,Item7表示Item1,Item2,Item3的值, Item4,Item5,Item6,Item7属性,如果任何属性包含空值,它将表示String.Empty。

句法:

public override string ToString ();

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

范例1:

// C# program to illustrate 
// the use of ToString method
using System;
  
namespace exampleofvaluetuple
{
    class GFG
    {
          
        // Main Method
        static void Main(string[] args)
        {
            // 1-ValueTuple
            var v1 = ValueTuple.Create("Rina");
              
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 1: " + v1.ToString());
  
            // 2-ValueTuple
            var v2 = ValueTuple.Create("Rohan", 25);
              
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 2: " + v2.ToString());
  
            // 3-ValueTuple
            var v3 = ValueTuple.Create("Rima", 22, 2016);
              
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 3: " + v3.ToString());
  
            // 4-ValueTuple
            var v4 = ValueTuple.Create("Mohit", 28, 2014, 
                                      "Junior Engineer");
                                        
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 4: " + v4.ToString());
  
        }
    }
}
输出:
ValueTuple 1: (Rina)
ValueTuple 2: (Rohan, 25)
ValueTuple 3: (Rima, 22, 2016)
ValueTuple 4: (Mohit, 28, 2014, Junior Engineer)

范例2:

// C# program to illustrate 
// the use of ToString method
using System;
  
namespace exampleofvaluetuple
{
    class GFG
    {
          
        // Main Method
        static void Main(string[] args)
        {
            // 5-ValueTuple
            var v5 = ValueTuple.Create("Rohit", 32, 2010,
                               "CSE", "Junior Engineer");
                                 
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 5: "+v5.ToString());
  
            // 6-ValueTuple
            var v6 = ValueTuple.Create("Sunita", 25, 2015,
                           "ECE", "Junior Engineer", 102);
                             
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 6: "+v6.ToString());
  
            // 7-ValueTuple
            var v7 = ValueTuple.Create("Sonu", 22, 2016,
                  "CSE", "Junior Engineer", 104, "C++");
                    
            // Get the value of ValueTuple
            // With the help of ToString method
            Console.WriteLine("ValueTuple 7: "+v7.ToString());
        }
    }
}
输出:
ValueTuple 5: (Rohit, 32, 2010, CSE, Junior Engineer)
ValueTuple 6: (Sunita, 25, 2015, ECE, Junior Engineer, 102)
ValueTuple 7: (Sonu, 22, 2016, CSE, Junior Engineer, 104, C++)