C# ToTuple() Method


Let’s say you have a ValueTuple and would like to convert it to a tuple, then use the ToTuple() method.

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

Let us see an example to implement ToTuple() method.

Example

using System;
class Program {
   static void Main() {
      var val = (1, 2, 3);
      //Add System.ValueTuple package to run this program
      // ValueTuple
      Console.WriteLine(“ValueTuple: ” val);
      // Tuple
      Tuple<int, int, int> myTuple = val.ToTuple();
      Console.WriteLine(“Tuple: ”+myTuple);
   }
}

Output

ValueTuple: (1, 2, 3)
Tuple: (1, 2, 3)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jun-2020

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements