Skip to content

ValueTuple

ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It provides strong naming conventions.

Tuple is of reference type, but ValueTuple is of value type.

Example

For example:

define

Define a value tuple:

var drinkTuple = (name: "coke", cty: 2);

Access item by ites name:

Console.WriteLine($"The drink {drinkTuple.name} have {drinkTuple.cty} items.");

use in function

public (string FirstName, string LastName) GetNames(string! fullName)
{
  string[] names = fullName.Split(" ", 2);
  return (names[0], names[1]);
}
public void Main()
{
  // ...
  (string first, string last) = GetNames("Inigo Montoya");
  // ...
}

Access tuple item by its name:

Console.WriteLine($”My name is { names.first } { names.last }.);