Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can we return multiple values from a function in C#?
In C#, there are several approaches to return multiple values from a function. This is useful when you need to return more than one result from a single method call.
The main approaches are −
-
Reference parameters using
refkeyword -
Output parameters using
outkeyword -
Returning an Array of values
-
Returning a Tuple object
Using Reference Parameters
The ref keyword passes arguments by reference, allowing the method to modify the original variable. The variable must be initialized before passing it to the method.
Syntax
returnType MethodName(type param1, ref type param2) {
// modify param2
return param1;
}
Example
using System;
class Program {
static int ReturnMultipleValuesUsingRef(int firstNumber, ref int secondNumber) {
secondNumber = 20;
return firstNumber;
}
static void Main() {
int a = 10;
int refValue = 0;
var res = ReturnMultipleValuesUsingRef(a, ref refValue);
Console.WriteLine($"Ref Value {refValue}");
Console.WriteLine($"Function Return Value {res}");
}
}
The output of the above code is −
Ref Value 20 Function Return Value 10
Using Output Parameters
The out keyword is similar to ref, but the variable doesn't need to be initialized before passing. The method must assign a value to the out parameter before returning.
Syntax
returnType MethodName(type param1, out type param2) {
param2 = value; // must assign
return param1;
}
Example
using System;
class Program {
static int ReturnMultipleValuesUsingOut(int firstNumber, out int secondNumber) {
secondNumber = 20;
return firstNumber;
}
static void Main() {
int a = 10;
int outValue;
var res = ReturnMultipleValuesUsingOut(a, out outValue);
Console.WriteLine($"Out Value {outValue}");
Console.WriteLine($"Function Return Value {res}");
}
}
The output of the above code is −
Out Value 20 Function Return Value 10
Returning Arrays
You can return multiple values by packaging them in an array. This approach works well when all return values have the same data type.
Example
using System;
class Program {
static int[] ReturnArrays() {
int[] arrays = new int[2] { 1, 2 };
return arrays;
}
static void Main() {
var res = ReturnArrays();
Console.WriteLine($"{res[0]} {res[1]}");
}
}
The output of the above code is −
1 2
Returning Tuples
Tuples provide a clean way to return multiple values of different types. The Tuple<T1, T2> class allows you to group multiple values together.
Example
using System;
class Program {
static Tuple<int, int> ReturnMultipleValuesUsingTuples() {
return new Tuple<int, int>(10, 20);
}
static void Main() {
var res = ReturnMultipleValuesUsingTuples();
Console.WriteLine($"{res.Item1} {res.Item2}");
}
}
The output of the above code is −
10 20
Comparison of Approaches
| Approach | Best Used When | Advantages | Disadvantages |
|---|---|---|---|
| ref parameters | Modifying existing variables | Direct variable modification | Variable must be initialized |
| out parameters | Creating new return values | No initialization required | Must assign before return |
| Arrays | Same data types | Simple for homogeneous data | Limited to same type |
| Tuples | Different data types | Supports mixed types | Less readable with Item1, Item2 |
Conclusion
C# offers multiple approaches to return several values from a function. Use out parameters for simple scenarios, arrays for homogeneous data, and tuples for mixed data types. Choose the approach that best fits your specific use case and data requirements.
