Return multiple values from a method in C#
This post will discuss how to return multiple values from a method in C#.
As per C# Language Specification, returning multiple values from a method is not possible. Using some other features offered by C#, we can return multiple values to the caller method. This post provides an overview of some of the available alternatives to accomplish this.
1. Using ref parameters
We can use the ref keyword to return a value to the caller by reference. We can use it to return multiple values from a method, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; public class Example { private static void fun(ref int x, ref int y) { x = 1; y = 2; } public static void Main() { int x = 0; int y = 0; fun(ref x, ref y); Console.WriteLine("x = {0}, y = {1}", x, y); } } /* Output: x = 1, y = 2 */ |
Note that the ref keyword won’t work with the Async and Iterator methods.
2. Using out parameter modifier
The out keyword causes arguments to be passed by reference. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed.
The following example demonstrates using the out parameter to return multiple values from a method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; public class Example { private static void fun(out int x, out int y) { x = 1; y = 2; } public static void Main() { int x = 0; int y = 0; fun(out x, out y); Console.WriteLine("x = {0}, y = {1}", x, y); } } /* Output: x = 1, y = 2 */ |
Note that the out parameter won’t work with the Async and Iterator methods.
3. Using Tuple Class
A tuple is a data structure that lets you easily package multiple values in a single object. Tuples are commonly used to return multiple values from a method.
The following example creates a 2-tuple and returns it from the fun() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public class Example { private static Tuple<int, int> fun() { return Tuple.Create(1, 2); } public static void Main() { Tuple<int, int> tuple = fun(); Console.WriteLine("x = {0}, y = {1}", tuple.Item1, tuple.Item2); } } /* Output: x = 1, y = 2 */ |
4. Using C#7 ValueTuple
Value tuples, introduced in .NET Framework 4.7, are tuple types to provide the runtime implementation of tuples in C#. Like tuple classes, We can use it to return multiple values from a method in a more effective way.
The following example uses type inference to deconstruct the 2-tuples returned by the method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public class Example { private static (int, int) fun() { return (1, 2); } public static void Main() { (int x, int y) = fun(); Console.WriteLine("x = {0}, y = {1}", x, y); } } /* Output: x = 1, y = 2 */ |
5. Using struct or Class
Here, the idea is to return an instance of a class containing all fields we want to return. The following code example returns multiple values from a method using struct.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; public class Example { private struct Pair { public int x; public int y; } private static Pair fun() { return new Pair { x = 1, y = 2 }; } public static void Main() { Pair pair = fun(); Console.WriteLine("x = {0}, y = {1}", pair.x, pair.y); } } /* Output: x = 1, y = 2 */ |
That’s all about returning multiple values from a method in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)