Recently, Microsoft announced various new features in C# 7.0 with Visual Studio 2017. In C# 7.0, some new feature of tuples are enhanced with Visual Studio 2017. Tuples aren’t new in C# or.NET. They were first introduced as a part of.NET Framework 4.0. This useful feature of the tuple is all about returning multiple value without having:

A class and list<> of the class.

Array, observableCollection, ArrayList or Dictionary.

A series of ref or out parameters.



It is similar to the code snippet given below.

public (string name, string dept, int id) GetData() { return ( "Avnish" , "R&D" , 001); }

to the code snippet given below.

Before C# 7.0, the use of tuple is given below.

public Tuple<string, string, int > GetData2() { return new Tuple<string, string, int >( "Rakesh" , "Marketing" , 002); }

You must include the System . ValueTuple Nuget package to take an advantage of it in your Visual studio 2017 or its community edition. This first thing to be noted is that it is not included automatically in a new project.must include the SystemValueTuple Nuget package to take an advantage of it in your Visual studio 2017 or itsedition.

Let's create a console project for demo.

Step 1



Create a new 'Console App' in Visual Studio 2017 RC or Visual Studio 2017.







Step 2



Go to add a NuGet packag e. By right click to "References" in Solution Explorer, click on " Manage NuGet Package ". Now, search for " System.ValueTuple " and install such package .







Step 3



For use of tuples , add the code given below in Program.cs.





namespace TupleDemo { class Program { static void Main(string[] args) { Demo demo = new Demo(); var result = demo.GetData(); Console.WriteLine( "Name= " + result.name + " Dept = " + result.dept + " Id= " + result.id); var result2 = demo.GetData2(); Console.WriteLine( "Name= " + result2.Item1 + " Dept = " + result2.Item2 + " Id= " + result2.Item3); var result3 = demo.GetData3(); Console.WriteLine( "Name= " + result3.Item1 + " Dept = " + result3.Item2 + " Id= " + result3.Item3); Console.ReadKey(); } } class Demo { public (string name, string dept, int id) GetData() { return ( "Avnish" , "R&D" , 001); } public Tuple<string, string, int > GetData2() { return new Tuple<string, string, int >( "Rakesh" , "Marketing" , 002); } public (string, string, int ) GetData3() { return ( "Mukesh" , "Testing" , 003); } } }

Step 4



Build and run the project.

