C# | ValueTuple <T1> Struct
The ValueTuple <T1> Struct is used to create a singleton value tuple or 1-ValueTuple which stores only one component. It provides runtime implementation of value tuples. You can create the instance of ValueTuple <T1> Struct by using ValueTuple<T1>(T1) constructor, or by using ValueTuple.Create method or simply by using parenthesis(). You can retrieve the value of the value tuple’s single unnamed element by using the default property, or named elements can directly be accessed with the help of their names.
Important Points:
- It implements IStructuralComparable, IStructuralEquatable, IComparable, IComparable<ValueTuple<T1>>, IEquatable<ValueTuple<T1>>, and ITuple interfaces.
- It defined under System namespace.
- It can also store duplicate elements.
- Fields are mutable. So, you can change the value of ValueTuple <T1>.
- Here, member like Item1 is field not property.
- It is of value type not of reference type.
Constructor
| Constructor | Description |
|---|---|
| ValueTuple<T1>(T1) | Initializes a new ValueTuple<T1> instance. |
Field
| Field | Description |
|---|---|
| Item1 | Gets the value of the current ValueTuple<T1> instance’s first element. |
Example:
// C# program to illustrate how to // access the element of ValueTuple<T1> using System; class GFG { // Main Method static public void Main() { // Creating a value tuple // Using Create method var Mylibrary = ValueTuple.Create(3456); // Display the element of the given value tuple Console.WriteLine("Book Id: {0}", Mylibrary.Item1); } } |
Book Id: 3456
Methods
| Method | Description |
|---|---|
| CompareTo(ValueTuple) | Compares the current ValueTuple<T1> instance to a specified ValueTuple<T1> instance. |
| Equals(Object) | Returns a value that indicates whether the current ValueTuple<T1> instance is equal to a specified object. |
| Equals(ValueTuple) | Returns a value that indicates whether the current ValueTuple<T1> instance is equal to a specified ValueTuple<T1> instance. |
| GetHashCode() | Calculates the hash code for the current ValueTuple<T1> instance. |
| ToString() | Returns a string that represents the value of this ValueTuple<T1> instance. |
Example:
// Check the given value tuples // are equal or not using System; class GFG { // Main method static public void Main() { // Creating 1-ValueTuple // Using Create method var T1 = ValueTuple.Create(346); var T2 = ValueTuple.Create(346); // Check if both the value tuples // are equal or not if (T1.Equals(T2)) { Console.WriteLine("Code is correct...!!"); } else { Console.WriteLine("Incorrect Code...!!"); } } } |
Code is correct...!!
Reference:
Recommended Posts:
- C# | ValueTuple <T1,T2> Struct
- C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7> Struct
- C# | ValueTuple <T1,T2,T3> Struct
- ValueTuple Struct in C#
- C# | ValueTuple <T1,T2,T3,T4,T5,T6> Struct
- C# | ValueTuple <T1,T2,T3,T4,T5> Struct
- C# | ValueTuple <T1,T2,T3,T4> Struct
- C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7,TRest> Struct
- ValueTuple in C#
- How to create 5-ValueTuple in C#?
- How to create 3-ValueTuple in C#?
- How to create 4-ValueTuple in C#?
- How to create 2-ValueTuple in C#?
- How to create 1-ValueTuple in C#?
- How to create 6-ValueTuple in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



