"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

C# 7 - Return multiple values from methods


It was a long-awaited feature to return multiple values from a method. Developers used collection variables such as Array, ArrayList or Generic List to return multiple values from methods. Now with Visual Studio 2017 and C# 7, you can easily return multiple values with the help of Tuples.

In this article, I am going to explain how tuples can be used in C# 7 onwards to return multiple values.

Consider the following code from the console application. The method GetDivisionResults accepts two parameters namely number and divisor and returns two integers that are quotient and remainder.

         static void Main(string[] args)
         {
             int number = 17;
             int devisor = 5;
             var result = GetDivisionResults(17, 5);
             Console.WriteLine("Quotient is " + result.Item1);
             Console.WriteLine("Remainder is " + result.Item2);
         }

        static (int, int) GetDivisionResults(int number, int divisor)
         {

            int quotient = number / divisor;
             int remainder = number % divisor;
             return (quotient, remainder);
         }

The following function definition defines a function that returns two integer values as tuples.

(int, int) GetDevisionREsults(int number, int devisor)

(int, int) – defines the return type of the method, which is a tuple contains two integers. I am not concerned about the logic of the application, but see how it returns two numbers.

return (quotient, remainder);

Cool! Right. Let us evaluate how we can access the return values.

Console.WriteLine("Quotient is " + result.Item1);

Console.WriteLine("Remainder is " + result.Item2);

As you can see the values returned are accessed by the relative position in the Tuple. But using .Item1, .Item2 etc. from Tuple variable is not friendly. Luckily C# 7 gives you an option to define the variable names in tuples. Consider the following example.

         static void Main(string[] args)
         {
             int number = 17;
             int devisor = 5;
             (int quotient, int remainder) = GetDivisionResults(17, 5);
             Console.WriteLine("Quotient is " + quotient);
             Console.WriteLine("Remainder is " + remainder);
         }

        static (int, int) GetDivisionResults(int number, int divisor)
         {

            int quotient = number / divisor;
             int remainder = number % divisor;
             return (quotient, remainder);
         }

The difference is very clear. The following statement defines the names for the returned values.

(int quotient, int remainder) = GetDivisionResults(17, 5);

Now you are no longer required to use .Item1 or .Item2. You can directly use the variable as follows.

Console.WriteLine("Quotient is " + quotient);

Console.WriteLine("Remainder is " + remainder);

This is a very cool feature that every programmer will love. Although I used only tuples with two variables, you can return more than two variables.

e.g. consider the signature of a method that returns first name, middle name and last name from a given name.

(string, string, string) GetNameParts(string fullname);

Note: If you are using .Net Framework 4.6.2 or earlier, you need to include the System.ValueTuple Nuget package, otherwise you will get the following error.

Predefined type 'System.ValueTuple`2' is not defined or imported

clip_image002

You can find the ValueTuple Nuget package from Microsoft and install it.

clip_image003

9 Comments

  • Nice article. Instead of Tuple or Class we can now use the new feature .

  • Fantastic a great little artcile that has helped me implement, and resolve the error: ie. nuget package!!

    Thank you.

  • Great post. Thanks for sharing!

  • Very helpful article!

  • Nice and Excellent post.. After reading this article i got c# value returns concepts clearly and program explanation are step by step so easy to understand.. thank you for sharing

    <a href="http://www.bestdotnettraininginchennai.com/">dot net training in chennai velachery</a>

  • Interesting feature in the latest version of C#. I am curious if on the backend if they are just converting the return values to a Tuple? Or maybe a dynamic? Either way this is a love hate feature. Can very easily enable sloppy code...

  • Preparing for the Project Management Professional (PMP) exam can be a daunting task, but with the right approach and resources, you can successfully pass the exam and earn your certification. One of the most effective ways to prepare for the PMP exam is by taking PMP Exam Dumps. These simulate the exam environment and help you gauge your readiness and identify areas for improvement.

  • thanks for the amazing content.

  • In-order to participate in Kroger Com Fuel Points Survey, keep the printed purchase receipt which you had got from your nearest stores. Before you start krogerfeedback.com survey, make sure the details you enter from the printed receipt was accurate. This Kroger.Com/Feedback Online Survey is Conducted by Kroger only to know the customer’s satisfaction on their product’s quality or services available at their largest grocery stores. In this Survey, just need to spend 5-10 minutes of your valuable time by visiting krogerfeedback. Fuel points are rewarded as an incentive. Share their valuable opinions and feedback by going to https://www.kroger.com/feedback helps them to know about their products. Every store like Kroger wants to know about customer satisfaction level with their products and services. Your Kroger.Com feedback survey could help them to improve their products and services. Based on your opinion, either they could alter the products and implement new functionalities to enhance the customer satisfaction.

Add a Comment

As it will appear on the website

Not displayed

Your website