How to Return Multiple Values From a Function in C#
- Return Multiple Values From a Function With the Arrays in C#
- Return Multiple Values From a Function With the Structure/Class Objects in C#
-
Return Multiple Values From a Function With the
Tuples<T1,T2>
Class in C# -
Return Multiple Values From a Function With the
(T1,T2)
Notation in C# - Conclusion
Navigating the landscape of C# programming often involves addressing the common challenge of returning multiple values from a function. It’s a common scenario to need more than just a single outcome from your code.
This tutorial explores efficient ways to achieve this, from the simplicity of arrays to the expressiveness of tuples and dedicated classes. We’ll delve into each method, showcasing its strengths and applications to empower you with the right tool for the job.
Return Multiple Values From a Function With the Arrays in C#
The array data structure stores multiple values of the same data type in C#. We can use arrays to return multiple values from a function. In this discussion, we’ll delve into the concept of returning multiple values using arrays in C#—a powerful technique that enhances code readability and maintainability.
using System;
public class MultipleValuesArrayExample {
public static object[] ReturnMultipleValuesArray() {
int intValue = 42;
string stringValue = "Array Method";
object[] resultArray = new object[] { intValue, stringValue };
return resultArray;
}
public static void Main() {
object[] result = ReturnMultipleValuesArray();
int resultInt = (int)result[0];
string resultString = (string)result[1];
Console.WriteLine($"Returned Integer: {resultInt}");
Console.WriteLine($"Returned String: {resultString}");
}
}
In the MultipleValuesArrayExample
class, we have a function named ReturnMultipleValuesArray
. This function is designed to return two values: an integer (intValue
) and a string (stringValue
).
Inside the function, we initialize intValue
to 42 and stringValue
to Array Method
. Then, we create an array named resultArray
of type object[]
.
The choice of the object
type allows us to store different data types within the same array.
We populate resultArray
with intValue
and stringValue
and return the array. Moving to the Main
method, we call ReturnMultipleValuesArray
and capture the returned array in the result
variable.
To extract the values from the array, we use casting. The first element of the array is cast to an integer (resultInt
), and the second element is cast to a string (resultString
).
Finally, we display the results on the console using Console.WriteLine
.
Output:
Returned Integer: 42
Returned String: Array Method
Return Multiple Values From a Function With the Structure/Class Objects in C#
The Structures and Classes are used to group related values. The values do not have to be of the same data type.
Hence, we can use a structure or a class object to return multiple values from a function in C#. This approach is particularly valuable when dealing with a set of related values that share a logical connection.
In this exploration, we’ll delve into the concept of returning multiple values using a class or structure in C#, showcasing how this method enhances code readability and maintainability.
using System;
public class MultipleValuesClassExample {
public class ReturnValue {
public int IntValue { get; set; }
public string StringValue { get; set; }
}
public static ReturnValue ReturnMultipleValuesClass() {
int intValue = 42;
string stringValue = "Class Method";
ReturnValue returnValue = new ReturnValue { IntValue = intValue, StringValue = stringValue };
return returnValue;
}
public static void Main() {
ReturnValue result = ReturnMultipleValuesClass();
int resultInt = result.IntValue;
string resultString = result.StringValue;
Console.WriteLine($"Returned Integer: {resultInt}");
Console.WriteLine($"Returned String: {resultString}");
}
}
In the MultipleValuesClassExample
class, we define an inner class named ReturnValue
.
This class serves as a container for the values we want to return from the function. It has two properties: IntValue
of type int
and StringValue
of type string
.
Moving to the ReturnMultipleValuesClass
function, we initialize two variables: intValue
and stringValue
. We then create an instance of the ReturnValue
class named returnValue
.
The values of intValue
and stringValue
are assigned to the corresponding properties of the returnValue
object.
Finally, the returnValue
object is returned from the function.
In the Main
method, we call ReturnMultipleValuesClass
and capture the returned returnValue
object in the result
variable.
To extract the values from the returned object, we access its properties: IntValue
and StringValue
.
The results are then displayed on the console using Console.WriteLine
.
Output:
Returned Integer: 42
Returned String: Class Method
Return Multiple Values From a Function With the Tuples<T1,T2>
Class in C#
Tuples are lightweight data structures that enable the bundling of multiple values into a single entity, facilitating clean and readable code. The Tuple<T1,T2>
class can be used to define custom tuples having user-defined data-types in a specific sequence.
Let’s explore a practical example of a C# function that returns multiple values using Tuple<T1, T2>
.
using System;
public class MultipleValuesTupleExample {
public static Tuple<int, string> ReturnMultipleValuesTuple() {
int intValue = 42;
string stringValue = "Tuple Method";
// Creating a Tuple with int and string values
Tuple<int, string> resultTuple = Tuple.Create(intValue, stringValue);
return resultTuple;
}
public static void Main() {
// Calling the function and capturing the returned Tuple
Tuple<int, string> resultTuple = ReturnMultipleValuesTuple();
// Extracting values from the Tuple
int resultInt = resultTuple.Item1;
string resultString = resultTuple.Item2;
// Displaying the results
Console.WriteLine($"Returned Integer: {resultInt}");
Console.WriteLine($"Returned String: {resultString}");
}
}
In the MultipleValuesTupleExample
class, we have a function named ReturnMultipleValuesTuple
. This function is designed to return two values: an integer (intValue
) and a string (stringValue
).
Inside the function, we initialize intValue
to 42 and stringValue
to Tuple Method
. The crucial step is creating a Tuple named resultTuple
using Tuple.Create(intValue, stringValue)
.
Moving to the Main
method, we call ReturnMultipleValuesTuple
and capture the returned Tuple in the resultTuple
variable.
To extract the values from the Tuple, we use the Item1
and Item2
properties, corresponding to the integer and string values, respectively.
Finally, we display the results on the console using Console.WriteLine
.
Output:
Returned Integer: 42
Returned String: Tuple Method
Return Multiple Values From a Function With the (T1,T2)
Notation in C#
The above section discussing how to use the Tuple<T1,T2>
class to simplify the process of returning multiple values from a function can be further shortened with the (T1, T2)
notation of declaring tuples. The (T1,T2)
notation does the same thing as the Tuple<T1,T2>
class by also shortening the code.
Let’s dive into a practical example of a C# function that returns multiple values using the (T1, T2)
notation.
using System;
public class MultipleValuesTupleNotationExample {
public static (int, string) ReturnMultipleValuesTuple() {
int intValue = 42;
string stringValue = "Tuple Notation Method";
return (intValue, stringValue);
}
public static void Main() {
// Calling the function and capturing the returned values using tuple deconstruction
var (resultInt, resultString) = ReturnMultipleValuesTuple();
// Displaying the results
Console.WriteLine($"Returned Integer: {resultInt}");
Console.WriteLine($"Returned String: {resultString}");
}
}
In the MultipleValuesTupleNotationExample
class, we have a function named ReturnMultipleValuesTuple
. This function is designed to return two values: an integer (intValue
) and a string (stringValue
).
Inside the function, we initialize intValue
to 42 and stringValue
to Tuple Notation Method
.
Now, here comes the elegance of the (T1, T2)
notation. Instead of explicitly using Tuple<int, string>
as the return type, we directly declare the return type as (int, string)
in the function signature.
Moving to the Main
method, we call ReturnMultipleValuesTuple
and use tuple deconstruction to capture the returned values in the variables resultInt
and resultString
.
Finally, we display the results on the console using Console.WriteLine
.
Output:
Returned Integer: 42
Returned String: Tuple Notation Method
Conclusion
We’ve covered versatile tools like arrays, tuples, and even custom classes, each offering unique advantages for data encapsulation and code organization. The array-return method in C#, as exemplified in the provided example, presents a direct and elegant solution for returning multiple values from a function.
Additionally, the exploration of using classes or structures and the (T1, T2)
notation further enriches the developer’s toolkit, offering organized approaches that enhance code clarity and maintainability in C# applications. Incorporating these techniques into your C# development promises a more streamlined and expressive coding experience.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn