How to create 4-ValueTuple in C#?
In C#, a quadruple or 4 value tuple is a value type tuple which holds four elements in it. You can create a quadruple value tuple using two different ways:
- Using ValueTuple <T1, T2, T3, T4>(T1, T2, T3, T4) Constructor
- Using Create <T1, T2, T3, T4>(T1, T2, T3, T4) method
Using ValueTuple <T1, T2, T3, T4>(T1, T2, T3, T4) Constructor
You can create a quadruple value tuple by using ValueTuple <T1, T2, T3, T4>(T1, T2, T3, T4) constructor. It initializes a new instance of the ValueTuple <T1, T2, T3, T4> struct. But when you create a value tuple using this constructor, then you have to specify the type of the element stored in the value tuple.
Syntax:
public ValueTuple (T1 item1, T2 item2, T3 item3, T4 item4);
Parameters:
- item1: It is the value of the first value tuple component.
- item2: It is the value of the second value tuple component.
- item3: It is the value of the third value tuple component.
- item4: It is the value of the fourth value tuple component.
Example:
// C# program to create a quadruple // value tuple using value tuple constructor using System; public class GFG { // Main method static public void Main() { // Creating a value tuple with four elements // Using ValueTuple<T1, T2, T3, T4>(T1, T2, T3, T4) constructor ValueTuple<string, string, string, string> MyTpl = new ValueTuple<string, string, string, string>("Dog", "Cat", "Cow", "Pig"); Console.WriteLine("Component 1: " + MyTpl.Item1); Console.WriteLine("Component 2: " + MyTpl.Item2); Console.WriteLine("Component 3: " + MyTpl.Item3); Console.WriteLine("Component 4: " + MyTpl.Item4); } } |
Component 1: Dog Component 2: Cat Component 3: Cow Component 4: Pig
Using Create <T1, T2, T3, T4>(T1, T2, T3, T4) Method
You can also create a quadruple value tuple or a value tuple which holds 4-elements with the help of Create <T1, T2, T3, T4>(T1, T2, T3, T4) method. When you use this method, then there is no need to specify the type of the elements stored in the value tuple.
Syntax:
public static ValueTuple<T1, T2, T3, T4> Create<T1, T2, T3, T4> (T1 item1, T2 item2, T3 item3, T4 item4);
Type Parameters:
- T1: It is the type of the value tuple’s first component.
- T2: It is the type of the value tuple’s second component.
- T3: It is the type of the value tuple’s third component.
- T4: It is the type of the value tuple’s fourth component.
Parameters:
- item1: It is the value of the value tuple’s first component.
- item2: It is the value of the value tuple’s second component.
- item3: It is the value of the value tuple’s third component.
- item4: It is the value of the value tuple’s fourth component.
Return Type: This method returns a value tuple with four elements.
Example:
// C# program to create a quadruple value tuple // using Create<T1, T2, T3, T4>(T1, T2, T3, // T4) method using System; class GFG { // Main method static public void Main() { // Creating a value tuple with four elements // Using Create<T1, T2, T3, T4>(T1, T2, T3, T4) // method var MyTple = ValueTuple.Create(12, 34, 56, 45); Console.WriteLine("Component 1: " + MyTple.Item1); Console.WriteLine("Component 2: " + MyTple.Item2); Console.WriteLine("Component 3: " + MyTple.Item3); Console.WriteLine("Component 4: " + MyTple.Item4); } } |
Component 1: 12 Component 2: 34 Component 3: 56 Component 4: 45
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.valuetuple-4.-ctor?view=netframework-4.8
- https://docs.microsoft.com/en-us/dotnet/api/system.valuetuple.create?view=netframework-4.8#System_ValueTuple_Create__4___0___1___2___3_
Recommended Posts:
- How to create a StringCollection in C#
- C# | How to create a SortedList
- How to create 1-ValueTuple in C#?
- How to create 2-ValueTuple in C#?
- How to Create a Range to a Specified End in C#?
- How to create a ListDictionary in C#
- How to create 6-Tuple in C#?
- How to create 5-ValueTuple in C#?
- C# | How to create a SortedSet
- How to create the StringBuilder in C#
- How to create a StringDictionary in C#
- How to create 8-ValueTuple in C#?
- How to create a Queue in C#
- How to create the ArrayList 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.


