-1

I am trying to code a little console game but I have a little problem.

In order to not write a function for every single decision the player will make, I need to know if there is a possibility to return multiple values in a single function. Below is a little example on what I have at the moment.

public static string three_Days_before()
{
    Console.Write("72 hours earlier...");
    Console.WriteLine(); //Just a little space.
    string first_sentence = "";
    Console.WriteLine(); //Just a little space.
    string second_sentence = "";
}

So here is what I have. And I want to know if I can somehow return both strings within the same function, without the need to write a function for every scene in order to be called in the Main() later.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Malgosh
  • 340
  • 1
  • 11
  • 7
    Yeah, you can use a Dictionary, Tuple, a Class, List. Depending on what exactly you need to return. – Adrian Nov 24 '19 at 19:58
  • Easy with downvotes guys, this is a new contributor. more than -1 is too much. Also the answer is was not duplicate, so maybe it is the same question but the answer is different :) – jimjim Nov 24 '19 at 21:56

1 Answers1

1

As of C# 7 you can return a ValueTuple with the following syntax:

public static (string, string) three_Days_before()
{
    Console.Write("72 hours earlier...");
    Console.WriteLine(); //Just a little space.
    string first_sentence = "";
    Console.WriteLine(); //Just a little space.
    string second_sentence = "";
    return (first_sentence, second_sentence);
}

You can optionally give them names too, the underlying type remains the same, but consuming it can be a bit nice as each item can have a name.

public static (string first_sentence, string second_sentence) three_Days_before()
{
    Console.Write("72 hours earlier...");
    Console.WriteLine(); //Just a little space.
    string first_sentence = "";
    Console.WriteLine(); //Just a little space.
    string second_sentence = "";
    return (first_sentence, second_sentence);
}
Stuart
  • 5,358
  • 19
  • 28
  • It is not at all evident that a `ValueTuple` is what he's looking for. The fact he is returning strings make me suspect it isn't. – Dour High Arch Nov 24 '19 at 20:05
  • 1
    I wasn't completely confident either, but later on they go on to say they want to return both, so that convinced me they were asking if multiple return values are supported. – Stuart Nov 24 '19 at 20:09
  • I agree with your assessment that the OP is looking to return two or more different values from one call to the method. However, Stack Overflow already has a much-loved Q&A covering this exact topic. There is no need whatsoever to post any new answers. You should've voted-to-close this post as a duplicate. – Peter Duniho Nov 24 '19 at 20:54
  • Thanks @PeterDuniho I will do so in future, I've only just got this privilege I think, so I'll start to put it into action from now on. – Stuart Nov 24 '19 at 20:58
  • Hi stuart. Thank you for the advice. The sevond method seems to work just fine. The only thing is, that the output I got came in between (). Is that normal when you call return (string_1, string_2); – Malgosh Nov 24 '19 at 21:04
  • @Malgosh You are probably calling .ToString on the result, this is how it shows. You will want to doing something with the pair of values, such as: Console.WriteLine($"{a.first_sentence}, {a.second_sentence}") – Stuart Nov 24 '19 at 21:10
  • very interesting approach, maybe question is duplicate but the answer is not :D – jimjim Nov 24 '19 at 21:54
  • 1
    @Stuart Oh, thanks mate! Worked as smooth as a baby bum! I really owe you fellas one. – Malgosh Nov 25 '19 at 09:10
  • 2
    @Arjang I am really sorry, if i posted a duplicate. I could not find an answer so I thought I'd post a question. – Malgosh Nov 25 '19 at 09:12
  • @Malgosh : haha, the irony is even if the question duplicate , this answer is not, so it was worth it. – jimjim Nov 25 '19 at 09:28