-6

String Interpolation

in this example, there is a variable declaration that I haven't seen before and don't know what it is:

var jh = (firstName: "Jupiter", lastName: "Hammon", born: 1711, published: 1761);

there is Anonymous Types in C# but it uses curly braces.

the jh.GetType() returns

System.ValueTuple 4[System.String,System.String,System.Int32,System.Int32]

but ValueTuple generics are not key-value pairs. they have some items. this syntax is more like a dictionary of named fields.

so my question is what is this syntax called? I'm looking for Microsoft docs page for getting documentation and details about this syntax.

Mokhabadi
  • 302
  • 2
  • 14
  • 3
    It's a [tuple](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples), the type tells you that which you could have googled. – DavidG Jan 09 '21 at 20:06
  • 1
    It's good that you're reading and citing documentation but you should use that same resource to learn just what that `System.ValueTuple` is. – Lance U. Matthews Jan 09 '21 at 20:11
  • 2
    You are asking 3 questions here. 1) what this code does? 2) what this technic is called? 3) how to replace var with an explicit type? For #1 and #2, see [Better naming in Tuple classes than “Item1”, “Item2”](https://stackoverflow.com/a/40013994/3744182) and [What's the difference between System.ValueTuple and System.Tuple?](https://stackoverflow.com/q/41084411/3744182). For #3 you need to clarify what you want for an "explicit type". Do you mean some type you have created, that you want to construct using this syntax? Or do you just want to know what type actually got created? – dbc Jan 09 '21 at 20:13

1 Answers1

1

As you already mentioned, this is ValueTuple. You can see here for some description and comparison with a "ordinal" Tuples. The official documentation also available here.

The code you showed is the just a typical declaration of such a ValueTuple. The syntax was introduced in C# 7.0.

To replace var, you can use the following:

(string firstName, string lastName, int born, int published) jh = (firstName: "Jupiter", lastName: "Hammon", born: 1711, published: 1761);
Serg
  • 3,454
  • 2
  • 13
  • 17
  • Can you explain the difference between `(string firstName, string lastName, int born, int published) jh = (firs....` and `System.ValueTuple jh = (firstName: "Jupiter", lastName: "Hammon", born: 1711, published: 1761);` ? – Luuk Jan 09 '21 at 20:18
  • 2
    The main advantage is that with `(string firstName,...) jh=` syntax you will get tuple with named fields, so you will be able to reference the members as `jh.firstName`. If you declare as `ValueTuple jh`, you will only able to use default names like `jh.Item1`. And yes, internally, the both records are absolutely identical - compiler will replace both with `ValueTuple` syntax. – Serg Jan 09 '21 at 21:48
  • Note that names are optional and that placing the names on both sides is not necessary. – Aluan Haddad Jan 09 '21 at 22:55
  • as @DavidG mentioned, This is `Tuple type` which requires the `System.ValueTuple` as in [Tuple types (C# reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples) says. – Mokhabadi Jan 11 '21 at 10:41