Skip to content

Instantly share code, notes, and snippets.

@fatihdgn
Last active September 30, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatihdgn/4ee6f2c291d3066d03b9787ce454dc52 to your computer and use it in GitHub Desktop.
Save fatihdgn/4ee6f2c291d3066d03b9787ce454dc52 to your computer and use it in GitHub Desktop.
//You can have multiple return values using tuples new syntax.
(string hello, string world) GetHelloWorldMessage()
{
// And create tuple with having your values in paranthesis.
return ("Hello", "World");
}
//You can deconstruct the resulting tuples values and assing them to different variables like this.
var (h, w) = GetHelloWorldMessage();
Console.Write($"{h} {w}");
//You can also assing your variables with type declarations.
var (string h2, string w2) = GetHelloWorldMessage();
Console.Write($"{h} {w}"); // This has the same output.
//Of course you can assing resulting tuple to a single tuple variable and use it like this.
var t = GetHelloWorldMessage();
Console.Write($"{t.hello} {t.world}"); // This has the same output too.
// These tuples are actually using the new ValueTuple struct and this struct actually have the "ItemX" properties inside like the old tuple do.
// So you can use these properties too if you want.
Console.Write($"{t.Item1} {t.Item2}"); // Guess what, this has the same output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment