I read the C++ version of this question but didn't really understand it.
Can someone please explain clearly if it can be done and how?
I read the C++ version of this question but didn't really understand it.
Can someone please explain clearly if it can be done and how?
No, you can't return multiple values from a function in C# (for versions lower than C# 7), at least not in the way you can do it in Python.
However, there are a couple alternatives:
You can return an array of type object with the multiple values you want in it.
You can use
out
parameters.you can try this "KeyValuePair"
Output :
Output : 1, 2
Future version of C# is going to include named tuples. Have a look at this channel9 session for the demo https://channel9.msdn.com/Events/Build/2016/B889
Skip to 13:00 for the tuple stuff. This will allow stuff like:
(incomplete example from video)
If you mean returning multiple values, you can either return a class/struct containing the values you want to return, or use the "out" keyword on your parameters, like so:
You can also use an OperationResult
Other answers stated using Tuple, which I would recommend too but using the new feature introduced in C# 7.0.
Further information can be found here.