Use C# 7.0 Tuples In Visual Studio 2017

Microsoft has announced a lot of new useful features that make C# development so easy and reusable. Also, Microsoft recently announced many new features in C# 7.0 along with Visual Studio 2017.

To know more about Visual Studio 2017, follow my last article Getting Started with Visual Studio 2017 Enterprise RC.

In this article, we will learn about Tuples, a new cool feature of C# 7.0, which, according to me, is the best among all of the new features.
 
Q. What is Tuples in C# 7.0?

Answer -

“When we want to return more than one value from a method anonymously, we can use Tuples in C# 7.0.”

Accordingly, Microsoft Official blogs.
  • Out parameters: Use is clunky (even with the improvements described above), and they don’t work with async methods.
  • System.Tuple<...> return types: Verbose to use and require an allocation of a tuple object.
  • Custom-built transport type for every method: A lot of code overhead for a type whose purpose is just to temporarily group a few values.
  • Anonymous types returned through a dynamic return type: High performance overhead and no static type checking.

Let’s have an example.

If you have to get more than one value outside from a function, then you had two options until C# 6.0.

  1. Return type should be a collection type like Array, List<>, ArrayList or Dictionary.



  2. We can use out or ref keyword with function arguments to get the changed value outside from function block.


Now, let’s see how we can use Tuples Type in C# 7.0 using Visual Studio 2017.

Start Visual Studio 2017.



Create New Project Console App->



After creating a project file (.cs), I am just trying to create a function in Test class, which is returning more than one value. But, here comes an issue - It shows me an error. Since we know that C# 7.0 allows us to return more than one value, there must be some solution. Well, here it is. If you want to use that behavior of C# 7.0, then you need to add a package from NuGet which is System.ValueTuple.
 
So, let’s go to use Value Tuple.



Right click on the project name into Solution Explorer and find "Manage NuGet Packages".



Now, select Browse for more online packages and find “System.ValueTuple” and install it.



Click to Install.



After installing System.ValueTuple, go to all references and see that you have a new reference.



Now, build your existing code. You will see that your code is now built successfully.



That means, you just need to add System.ValueTuple into your project and you are able to use Tuple as Value type data type. Let’s see now, how to call that function and get these values.

When you try to call this function that is returning Tuple type, just create a normal object of your class and call that function when you want to call it. So, in the Visual Studio intelliSense, you can see that function gives more than one value.



I just called that function and held the return value into a var type variable. You have many ways to use it.
  1. When you directly return more than one value into a Tuple type, it gives them some default name as per their order so as to call them easily. Lets's take the above example. I just tried to return two values - string type and bool type.

     So, it gave them names, by default, like- Item1 and Item2 as per their order into return type. Thus, you can use them by the names Item1 or Item2.



    I have already written the code to test them. So, just build the project, debug it, and wait for the output.



    See the output.



  2. Tuple type variables can have custom name instead of Item1 or Item2. As you can see in the following code, I have already given some names to those variables.



    Now, when you use it again, you will see their custom names in Visual Studio intelliSense.





    Now, build your app and wait for output.



  3. There is one more option to get the value of Tuple type. If you already have assigned a custom name, but you forget their names somehow, still you can call them by their order, like in following example.
    I have given a name to the first value but still I can call that by Item1 because that was on first position. Similarly, if you want to call IsActivate’s value, you can use Item2 . However, it will not be visible into Visual Studio intelliSense and you will have to type it manually.



    1. namespace ConsoleApp1  
    2. {  
    3.     class Program  
    4.     {  
    5.         static void Main(string[] args)  
    6.         {  
    7.             Test _Object = new Test();  
    8.             var data = _Object.TestFunction();  
    9.             Console.WriteLine(data.Name);  
    10.             Console.WriteLine(data.Item1);  
    11.             Console.ReadLine();  
    12.         }  
    13.     }  
    14.     class Test  
    15.     {  
    16.         public (string Name,bool IsActivate) TestFunction()  
    17.         {  
    18.             return ("Nitin Pandit"true);  
    19.         }  
    20.     }  
    21.   

    Output is the same as you call by Name it’s same as Item1:



  4. One more way to use Tuple type - If you want to use these values returned by function individually instead of using them like an object’s property, you can hold them in a var type as in the following code and can use them individually.



    1. namespace ConsoleApp1  
    2. {  
    3.     class Program  
    4.     {  
    5.         static void Main(string[] args)  
    6.         {  
    7.             Test _Object = new Test();  
    8.             var (Name,IsActivate) = _Object.TestFunction();  
    9.             Console.WriteLine(Name);  
    10.             Console.WriteLine(IsActivate);  
    11.             Console.ReadLine();  
    12.         }  
    13.     }  
    14.     class Test  
    15.     {  
    16.         public (string Name,bool IsActivate) TestFunction()  
    17.         {  
    18.             return ("Nitin Pandit"true);  
    19.         }  
    20.     }  
    21.   

Output still will be the same.



That was all about Tuples in C# 7.0 in Visual Studio 2017.

Thanks for reading this article. Stay tuned with me for more updates about C# 7.0 or Visual Studio 2017.

Connect (“Nitin Pandit”);

Up Next
    Ebook Download
    View all
    Learn
    View all