I read the C++ version of this question but didn't really understand it.
Can someone please explain clearly if it can be done in C#, and how?
You can return only one value in Java. If needed you can return multiple values using array or an object.
Answer: The value-returning function returns only one value by using the return statement.
You can return an object of a Class in Java. If you are returning more than 1 value that are related, then it makes sense to encapsulate them into a class and then return an object of that class. If you want to return unrelated values, then you can use Java's built-in container classes like Map, List, Set etc.
In C# 7 and above, see this answer.
In previous versions, you can use .NET 4.0+'s Tuple:
For Example:
public Tuple<int, int> GetMultipleValue() { return Tuple.Create(1,2); }
Tuples with two values have Item1
and Item2
as properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With