I have a function where I want to return two values. Is this possible?
This is my code, but it doesn't seem to like that I want to return two values:
public string PlayerCards(string player1C1, string player1C2)
{
generatedCard = randomCard.Next(1, 52);
player1C1 = generatedCard.ToString();
player1C1 = player1C1 + ".png";
return player1C1, player1C2;
}
Some options:
Use an out
parameter:
public string PlayerCards(out string x)
Return one value, and set the out
parameter (x
in this case) to another value; the calling code will need to specify an argument with out
as well, and after the call has completed, the caller will be able to see the value set in the method.
(It's not clear why you're accepting parameters at all; you don't seem to really use them.)
Return a ValueTuple<string, string>
, ideally using C# 7 tuples to provide element names
Tuple<string, string>
PlayingCard
type.It's not at all clear what your code is trying to do - the name of the method isn't clear and you don't use the parameters. When you've clarified what the method is trying to achieve - to yourself as much as to us - the answer may well become more obvious.
I'd also encourage you to use local variables where appropriate - I suspect generatedCard
should be a local variable instead of the (presumably) instance variable it currently is.