Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# | ValueTuple <T1,T2,T3,T4,T5> Struct

  • Last Updated : 23 Jul, 2019

The ValueTuple <T1,T2,T3,T4,T5> Struct is used to create a quintuple or 5-ValueTuple. It represents a value tuple which stores, five elements. It provides runtime implementation of value tuples. You can create the instance of ValueTuple <T1,T2,T3,T4,T5> by using ValueTuple<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5) constructor, or by using ValueTuple.Create method or simply by using parenthesis(). You can retrieve the value of the value tuple’s unnamed elements 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, T2, T3, T4, T5>>, IEquatable<ValueTuple<T1, T2, T3, T4, T5>>, 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, T2, T3, T4, T5>.
  • Here, members like Item1, Item2, Item3, Item4, and Item5 are fields not property.
  • It is of value type not of reference type.
  • It represent multiple data into a single data set.
  • It allows passing multiple values to a method with the help of single parameters.

Constructor

ConstructorDescription
ValueTuple<T1, T2, T3, T4, T5>(T1, T2, T3, T4, T5)Initializes a new ValueTuple<T1, T2, T3, T4, T5> instance.

Field

FieldDescription
Item1Gets the value of the current ValueTuple<T1, T2, T3, T4, T5> instance’s first element.Item2Gets the value of the current ValueTuple<T1, T2, T3, T4, T5> instance’s second element.Item3Gets the value of the current ValueTuple<T1, T2, T3, T4, T5> instance’s third element.Item4Gets the value of the current ValueTuple<T1, T2, T3, T4, T5> instance’s fourth element.Item5Gets the value of the current ValueTuple<T1, T2, T3, T4, T5> instance’s fifth element.

Example:




// C# program to illustrate how to
// access the element of ValueTuple<T1, 
// T2, T3, T4, T5>
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating a value tuple
        // Using Create method
        var Mylibrary = ValueTuple.Create(3456, "The Guide"
             "R. K. Narayan", 1958, "Philosophical novel");
  
        // Display the element of the given value tuple
        Console.WriteLine("Book Details: ");
        Console.WriteLine("Book Id: {0}", Mylibrary.Item1);
        Console.WriteLine("Book Name: {0}", Mylibrary.Item2);
        Console.WriteLine("Author Name: {0}", Mylibrary.Item3);
        Console.WriteLine("Publication date: {0}", Mylibrary.Item4);
        Console.WriteLine("Gener: {0}", Mylibrary.Item5);
    }
}
Output:
Book Details: 
Book Id: 3456
Book Name: The Guide
Author Name: R. K. Narayan
Publication date: 1958
Gener: Philosophical novel

Methods

MethodDescription
CompareTo(ValueTuple)Compares the current ValueTuple<T1, T2, T3, T4, T5> instance to a specified ValueTuple<T1, T2, T3, T4, T5> instance.
Equals(Object)Returns a value that indicates whether the current ValueTuple<T1, T2, T3, T4, T5> instance is equal to a specified object.
Equals(ValueTuple)Returns a value that indicates whether the current ValueTuple<T1, T2, T3, T4, T5> instance is equal to a specified ValueTuple<T1, T2, T3, T4, T5> instance.
GetHashCode()Calculates the hash code for the current ValueTuple<T1, T2, T3, T4, T5> instance.
ToString()Returns a string that represents the value of this ValueTuple<T1, T2, T3, T4, T5> instance.



Example:




// C# program to check the given value
// tuples are equal or not
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating 5-ValueTuple
        // using Create method
        var T1 = ValueTuple.Create(346, 784, 45, 22, 2);
        var T2 = ValueTuple.Create(346, 7743, 56, 22, 1);
  
        // Check if both the value 
        // tuples are equal or not
        if (T1.Equals(T2)) 
        {
            Console.WriteLine("Code is correct...!!");
        }
  
        else 
        {
            Console.WriteLine("Incorrect Code...!!");
        }
    }
}
Output:
Incorrect Code...!!

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :