introduction
According to the C# language specification, it is impossible to return multiple values from a method. Using some other features provided by C#, we can return multiple values to the caller method. This article outlines some available alternatives to achieve this.
1. Use the ref parameter
We can useref
Keywords Return values to the caller by reference. We can use it to return multiple values from a method,
As shown below:
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); ("x = {0}, y = {1}", x, y); } } /* Output: x = 1, y = 2 */
Please note thatref
Keywords do not apply toAsync
andIterator
method.
2. Use out parameter modifier
out
Keywords cause parameters to be passed by reference. It's like the ref keyword, except that ref requires that it be initialized before passing the variable.
The following example demonstrates using the out parameter to return multiple values from a method.
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); ("x = {0}, y = {1}", x, y); } } /* Output: x = 1, y = 2 */
Note that the out parameter does not apply to Async and Iterator methods.
3. Use tuple classes
A tuple is a data structure that allows you to easily package multiple values into a single object. Tuples are usually used to return multiple values from a method.
The following example creates a 2 tuple and from the fun() method:
using System; public class Example { private static Tuple<int, int> fun() { return (1, 2); } public static void Main() { Tuple<int, int> tuple = fun(); ("x = {0}, y = {1}", tuple.Item1, tuple.Item2); } } /* Output: x = 1, y = 2 */
tuple
It is a tuple that supports up to 7 elements, and any number of them need to be implemented in nesting and other methods.
The method of defining a function using tuples is as follows:
public static Tuple<string,string> TupleFun() { string[] T = {'hello','world'}; Tuple<string, string> tup = new Tuple<string, string>(T[0], T[1]); return tup; }
Tuples also support multiple types of values.
public static Tuple<string,int> TupleFun() { string T = ‘hello'; int q = 6; Tuple<string, int> tup = new Tuple<string, int>(T, q); return tup; }
When calling a function, use Item* to call elements within the tuple.
var tuple = TupleFun(); print(tuple.Item1); print((tuple.Item2));
4. Use C#7 ValueTuple
Value tuples, introduced in .NET Framework 4.7, are tuple types that are used to provide runtime implementations of tuples in C#. Like a tuple class, we can use it to return multiple values from a method in a more efficient way.
The following example uses type inference to deconstruct the 2 tuple returned by the method.
using System; public class Example { private static (int, int) fun() { return (1, 2); } public static void Main() { (int x, int y) = fun(); ("x = {0}, y = {1}", x, y); } } /* Output: x = 1, y = 2 */
5. Use structure or class
Here, the idea is to return an instance of a class that contains all the fields we want to return. The following code example returns multiple values from a method using struct.
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(); ("x = {0}, y = {1}", , ); } } /* Output: x = 1, y = 2 */
This is all about returning multiple values from a method in C#.
This is the end of this article about the details of how C# function returns multiple values. For more related contents of C# function return values, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!