Convert a ValueTuple to a Tuple in C#


With C#, we can easily convert a ValueTuple to a Tuple using ToTuple() method.

Note − Add System.ValueTuple package to run ValueTuple program.

Let’s see how to add it −

  • Go to your project
  • Right click on the project in the solution explorer
  • Select “Manage NuGet Packages”
  • You will reach the NuGet Package Manager.
  • Now, click the Browse tab and find “ValueTuple”
  • Finally, add System.ValueTuple package

Example

using System;
class Program {
   static void Main() {
      var val = (5, 50, 500, 5000);
      //Add System.ValueTuple package to run this program
      // ValueTuple
      Console.WriteLine(“ValueTuple: ” val);

      // Tuple
      Tuple<int, int, int, int> myTuple = val.ToTuple();
      Console.WriteLine(“Tuple: ”+myTuple);
   }
}

Output

ValueTuple: (5, 50, 500, 5000)
Tuple: (5, 50, 500, 5000)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements