Skip to content
Related Articles

Related Articles

Improve Article

How to create 3-ValueTuple in C#?

  • Last Updated : 23 Jul, 2019

In C#, a triple or 3 value tuple is a value type tuple which holds three elements in it. You can create a triple value tuple using two different ways:

  1. Using ValueTuple <T1, T2, T3>(T1, T2, T3) Constructor
  2. Using Create <T1, T2, T3>(T1, T2, T3) Method

Using ValueTuple <T1, T2, T3>(T1, T2, T3) Constructor

You can create a triple value tuple by using ValueTuple <T1, T2, T3>(T1, T2, T3) constructor. It initializes a new instance of the ValueTuple <T1, T2, T3> 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);

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.

Example:






// C# program to create a triple
// value tuple using value tuple constructor
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuple with three elements
        // Using ValueTuple<T1, T2, T3>(T1, T2, T3) constructor
        ValueTuple<string, string, string> MyTpl = new ValueTuple<string,
                                    string, string>("Dog", "Cat", "Cow");
  
        Console.WriteLine("Component 1: " + MyTpl.Item1);
        Console.WriteLine("Component 2: " + MyTpl.Item2);
        Console.WriteLine("Component 3: " + MyTpl.Item3);
    }
}
Output:
Component 1: Dog
Component 2: Cat
Component 3: Cow

Using Create <T1, T2, T3>(T1, T2, T3) Method

You can also create a triple value tuple or a value tuple which holds 3-elements with the help of Create <T1, T2, T3>(T1, T2, T3) 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> Create<T1, T2, T3> (T1 item1, T2 item2, T3 item3);

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.

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.

Returns: This method returns a value tuple with three elements.

Example:




// C# program to create a tuple value tuple
// using Create<T1, T2, T3>(T1, T2, T3) method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating a value tuple with three elements
        // Using Create<T1, T2, T3>(T1, T2, T3) method
        var MyTple = ValueTuple.Create(12, 34, 56);
  
        Console.WriteLine("Component 1: " + MyTple.Item1);
        Console.WriteLine("Component 2: " + MyTple.Item2);
        Console.WriteLine("Component 3: " + MyTple.Item3);
    }
}
Output:
Component 1: 12
Component 2: 34
Component 3: 56

Reference:




My Personal Notes arrow_drop_up
Recommended Articles
Page :