为什么我们需要 ValueTuple?
- Tuple 是引用类型,但 ValueTuple 是值类型。
- Tuple 不提供命名约定,但 ValueTuple 提供了强大的命名约定。
- 在 Tuples 中,您不能创建零分量的元组,但在 ValueTuple 中,您可以创建零元素的元组。
- ValueTuple 的性能优于 Tuple。 因为 ValueTuple 提供了一种轻量级的机制,用于从现有方法返回多个值。 并且 ValueTuple 的语法比 Tuples 更优化。
- ValueTuple 通过使用解构和 _ 关键字为访问值元组的元素提供了更大的灵活性。 但是 Tuple 不能提供解构的概念和 _ 关键字。
- 在 ValueTuple 中,item1 和 item2 等成员是字段。 但在元组中,它们是属性。
- 在 ValueTuple 中,字段是可变的。 但是在元组中,字段是只读的。
Creating a ValueTuple
-
Using Constructor
// Constructor for creating one element
ValueTuple(T1)
// Constructor for creating two elements
ValueTuple<T1, T2>(T1, T2)
// Constructor for creating eight elements
ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest) /
Sample
using System;
class GFG {
// Main method
static public void Main()
{
// ValueTuple with one element
ValueTuple<int> ValTpl1 = new ValueTuple<int>(345678);
// ValueTuple with three elements
ValueTuple<string, string, int> ValTpl2 = new ValueTuple<string,
string, int>("C#", "Java", 586);
// ValueTuple with eight elements
ValueTuple<int, int, int, int, int, int, int, ValueTuple<int> > ValTpl3 = new ValueTuple<int,
int, int, int, int, int, int, ValueTuple<int> >(45, 67, 65, 34, 34,
34, 23, new ValueTuple<int>(90));
}
}
- Using Create Method
// 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, TRest>(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");
}
}
- Using parenthesis()
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<int, string, string> author = (20, "Siya", "Ruby");
}
}
Accessing ValueTuple members
- Accessing unnamed members
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);
}
}
- Accessing Named members:
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);
}
}
Returning ValueTuple
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}");
}
}