If you are working with C# then there is a high possibility, that you may want to return multiple values from a function, so in this article, I have mentioned how we can return multiple values from a method in C# using console application example.
Using Ref parameters
This approach is useful if you are using .NET version below 4, so basically this is older approach but can be used with new .NET technology so.
Reference parameters, also known as "ref" parameters is one of the oldest and common way to return multiple values from a function.
Here is the example to return multiple values using ref
keyword.
using System;
namespace ReturnMultipleValues
{
class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 10;
int addResult = 0;
int multiplyResult = 0;
AddMultiply(a, b, ref addResult, ref multiplyResult);
Console.WriteLine(addResult);
Console.WriteLine(multiplyResult);
}
private static void AddMultiply(int a, int b, ref int add, ref int multiply)
{
add = a + b;
multiply = a * b;
}
}
}
Output:
15
50
ref parameters do not work, if you plan to use async/await functionality.
Using Out parameters
We can follow similar approach as above but with using output parameters. Output parameters also known as "out" parameters and are similar to reference parameters.
As the name suggests, they are passed to the function as parameters and the calling method expects some values to be passed back in the parameter from the function.
Here is an example, same as "ref" parameters example, instead we are using just "out" keyword, as output parameters.
using System;
namespace ReturnMultipleValues
{
class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 10;
int addResult = 0;
int multiplyResult = 0;
AddMultiply(a, b, out addResult, out multiplyResult);
Console.WriteLine(addResult);
Console.WriteLine(multiplyResult);
}
private static void AddMultiply(int a, int b, out int add, out int multiply)
{
add = a + b;
multiply = a * b;
}
}
}
Output:
15
50
Again, out parameters doesn't work with async/await.
Using Tuples
In .NET 4.0, a set of Tuple classes has been introduced in the framework, which can be usedto return multiple values from a methods easily and also makes code readable.
using System;
namespace ReturnMultipleValues
{
class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 10;
var result = AddMultiply(a, b);
Console.WriteLine(result.Item1);
Console.WriteLine(result.Item2);
}
private static Tuple<int, int> AddMultiply(int a, int b)
{
var tuple = new Tuple<int, int>(a + b, a * b);
return tuple;
}
}
}
Output:
15
50
Some of the points, related to tuples:
- Tuple can take up to 8 values.
- Can be nested for more than 8 values.
- It is a reference type, not a value type.
- It is inherited from the Object class.
- Works with Async/await.
So it is better to use Tuples if you are working with new C# version and adding new code in your project.
You may also like to read:
Add values in C# array (Multiple ways)
How to Comment Code in Powershell