Loading, please wait...

A to Z Full Forms and Acronyms

Use C# 7.0 Tuples In Visual Studio 2017

Mar 21, 2018 C#, C# 7.0, Visual Studio 2017, 5655 Views
In this article, you will learn how to 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 of a function, then you had two options until C# 6.0.

    1. The 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 of 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. Let'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 a 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 the following example. 
    I have given a name to the first value but still, I can call that by Item1 because that was in the first position. Similarly, if you want to call IsActivate’s value, you can use Item2. However, it will not be visible in Visual Studio IntelliSense and you will have to type it manually.



     

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


    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Test _Object = new Test();  
                var data = _Object.TestFunction();  
                Console.WriteLine(data.Name);  
                Console.WriteLine(data.Item1);  
                Console.ReadLine();  
            }  
        }  
        class Test  
        {  
            public (string Name,bool IsActivate) TestFunction()  
            {  
                return ("Nitin Pandit", true);  
            }  
        }  
      
    } ​
  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.



     
    namespace ConsoleApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Test _Object = new Test();  
                var (Name,IsActivate) = _Object.TestFunction();  
                Console.WriteLine(Name);  
                Console.WriteLine(IsActivate);  
                Console.ReadLine();  
            }  
        }  
        class Test  
        {  
            public (string Name,bool IsActivate) TestFunction()  
            {  
                return ("Nitin Pandit", true);  
            }  
        }  
      
    }  ​

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”);

A to Z Full Forms and Acronyms