Using Constructor
// Constructor for creating one element
ValueTuple(T1)
// Constructor for creating two elements
ValueTuple(T1, T2)
// Constructor for creating eight elements
ValueTuple(T1, T2, T3, T4, T5, T6, T7, TRest) /
using System;
class GFG {
// Main method
static public void Main()
{
// ValueTuple with one element
ValueTuple ValTpl1 = new ValueTuple(345678);
// ValueTuple with three elements
ValueTuple ValTpl2 = new ValueTuple("C#", "Java", 586);
// ValueTuple with eight elements
ValueTuple > ValTpl3 = new ValueTuple >(45, 67, 65, 34, 34,
34, 23, new ValueTuple(90));
}
}
// Method for creating an empty value tuple
Create();
// Method for creating 1-ValueTuple
Create(T1)
// Method for creating 8-ValueTuple
Create(T1, T2, T3, T4, T5, T6, T7, T8)
Sample
using System;
public class GFG {
// Main method
static public void Main()
{
// Creating 0-ValueTuple
// Using Create() Method
var Valtpl1 = ValueTuple.Create();
// Creating 3-ValueTuple
// Using Create(T1, T2, T3) Method
var Valtpl2 = ValueTuple.Create(12, 30, 40, 50);
// Creating 8-ValueTuple
// Using Create(T1, T2, T3, T4, T5, T6, T7, T8) Method
var Valtpl3 = ValueTuple.Create(34, "GeeksforGeeks",
'g', 'f', 'g', 56.78, 4323, "geeks");
}
}
named member
using System;
public class GFG {
static public void Main()
{
(int age, string Aname, string Lang) author = (23, "Sonia", "C#");
}
}
using System;
public class GFG {
static public void Main()
{
var author = (age : 23, Aname
: "Sonia", Lang
: "C#");
}
}
UnNamed Member
using System;
public class GFG {
static public void Main()
{
var author = (20, "Siya", "Ruby");
}
}
using System;
public class GFG {
static public void Main()
{
ValueTuple author = (20, "Siya", "Ruby");
}
}
using System;
public class GFG {
// Main Method
static public void Main()
{
// ValueTuple with three elements
var author = (20, "Siya", "Ruby");
// Accessing the ValueTuple
// Using default Item property
Console.WriteLine("Age:" + author.Item1);
Console.WriteLine("Name:" + author.Item2);
Console.WriteLine("Language:" + author.Item3);
}
}
using System;
public class GFG {
// Main Method
static public void Main()
{
// ValueTuple with three elements
var library = (Book_id : 2340, Author_name
: "Arundhati Roy", Book_name
: "The God of Small Things");
// Accessing the ValueTuple
// according to their names
Console.WriteLine("Book Id: {0}", library.Book_id);
Console.WriteLine("Author Name: {0}", library.Author_name);
Console.WriteLine("Book Name: {0}", library.Book_name);
}
}
using System;
public class GFG {
// This method returns the tourist details
static(int, string, string) TouristDetails()
{
return (384645, "Sophite", "USA");
}
// Main method
static public void Main()
{
// Store the data provided by the TouristDetails method
var(Tourist_Id, Tourist_Name, Country) = TouristDetails();
// Display data
Console.WriteLine("Tourist Details: ");
Console.WriteLine($ "Tourist Id: {Tourist_Id}");
Console.WriteLine($ "Tourist Name: {Tourist_Name}");
Console.WriteLine($ "Country: {Country}");
}
}