Getting the String that Represent the Value of ValueTuple<T1,T2,T3> Instance in C#
Last Updated :
11 Dec, 2019
ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the value of the ValueTuple's object with the help of the
ToString Method.
This method returns a string that will represent the value of the ValueTuple<T1, T2, T3> object. The string represented by this method is in the form of (Item1, Item2, Item3) here Item1, Item2, Item3 represent the values of Item1, Item2, Item3 properties, and it will represent a
String.Empty if any property contains a null value.
Syntax:
public override string ToString ();
Return Type: The return type of this method is
System.String. So, it will return a string that represents ValueTuple<T1, T2, T3> object.
Example 1:
CSharp
// 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<T1>
// 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<T1, T2>
// 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<T1, T2, T3>
// With the help of ToString method
Console.WriteLine("ValueTuple 3: " + v3.ToString());
}
}
}
Output:
ValueTuple 1: (Rina)
ValueTuple 2: (Rohan, 25)
ValueTuple 3: (Rima, 22, 2016)
Example 2:
CSharp
// C# program to illustrate
// the use of ToString method
using System;
namespace exampleofvaluetuple {
class GFG{
// Main Method
static void Main(string[] args)
{
// Nested Value Tuples
var Emp1 = (Name:"Anu", Age: 23, Languages:ValueTuple.Create("C++",
"Java", "Python", "C#"));
var Emp2 = (Name:"Boond", Age:27, Post: "Junior Engineer",
Languages:ValueTuple.Create("C++", "Java"));
var Emp3 = (Name: "Rohit", Age: 25, Post: "HR",
Languages: ValueTuple.Create("C++",
"Java", "C#"));
var Emp4 = (Name: "Mohan", Age: 26, Post: "Junior Engineer",
Languages: ValueTuple.Create("C++", "Java", "Python"));
// Get the value of Nested ValueTuples
// With the help of ToString method
Console.WriteLine("NValueTuple 1: {0}", Emp1.ToString());
Console.WriteLine("NValueTuple 2: {0}", Emp2.ToString());
Console.WriteLine("NValueTuple 3: {0}", Emp3.ToString());
Console.WriteLine("NValueTuple 4: {0}", Emp4.ToString());
}
}
}
Output:
NValueTuple 1: (Anu, 23, (C++, Java, Python, C#))
NValueTuple 2: (Boond, 27, Junior Engineer, (C++, Java))
NValueTuple 3: (Rohit, 25, HR, (C++, Java, C#))
NValueTuple 4: (Mohan, 26, Junior Engineer, (C++, Java, Python))
Similar Reads
Getting the String that Represent the Value of ValueTuple<T1,T2,T3,T4> Instance in C# ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the value of the ValueTuple's object with the help of the ToSt
3 min read
Getting the String that Represent the Value of ValueTuple<T1,T2> Instance in C# ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the value of the ValueTuple's object with the help of the ToSt
2 min read
Getting the String that Represent the Value of ValueTuple<T1> Instance in C# ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the value of the ValueTuple's object with the help of the ToSt
2 min read
Getting the String that Represent the Value of ValueTuple<T1,T2,T3,T4,T5,T6> Instance in C# ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the value of the ValueTuple's object with the help of the ToSt
3 min read
Getting the String that Represent the Value of the ValueTuple<T1,T2,T3,T4,T5> Instance in C# ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It allows you to store a data set that contains multiple values that may or may not be related to each other. You can also get a string that represents the value of the ValueTuple's object with the help of the ToSt
3 min read