1

I came across use of code like this in a codebase that I'm working on:

private (string someVar1, string someVar2) ParseInfo(Info info)
{
    return (someVar1: "test1", someVar2: "test2");
}

Does the approach above basically provide a simplified approach for returning a complex object on the fly without having to create a special class/structure? What is this construct named and when was it introduced?

3 Answers3

1

It is called 'Value Tuples' and it was introduced in C# 7.0 It is a value type representation of the tuple object that you can use in order to return values

More info here: https://blogs.msdn.microsoft.com/mazhou/2017/05/26/c-7-series-part-1-value-tuples/

Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

"C# 7.0 introduced ValueTuple structure, which is a value type representation of the tuple object. The language team made many good things for this value tuple type, including a new syntax and many features (such as deconstruction.)

Note that if you don’t see the ValueTuple available in your project, you have to download the System.ValueTuple 4.3.0 NuGet package to your project. You don’t need to do anything if you are using .NET Framework 4.7 or higher, or .NET Standard Library 2.0 or higher."

https://blogs.msdn.microsoft.com/mazhou/2017/05/26/c-7-series-part-1-value-tuples/

Falco Alexander
  • 3,092
  • 2
  • 20
  • 39
-4
  • Technically it returns a struct. Note that this means no garbage collection. And yes, it is easier than

  • This is called a "TUPLE". It was introduced with C# 7.0

More details among other things:

Does C# support multiple return values?

And a good explanation at

https://blogs.msdn.microsoft.com/mazhou/2017/05/26/c-7-series-part-1-value-tuples/

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • "This is called a "TUPLE". It was introduced with C# 7.0" Tuples were introduced a loooooooong time ago – Camilo Terevinto Jan 22 '19 at 17:41
  • Who says that structs are not garbage collected? If you have an array of ints, is it not garbage collected? If you have a class with an int field, is it not garbage collected? If you have a tuple with a string in it, is the string not garbage collected? – Eric Lippert Jan 22 '19 at 18:35