C# 7.0 – #3. New Tuple value type

First of all, at least for C# 7.0 included in Visual Studio 2017 RC, it needs to download the System.ValueTuple Nuget Package, providing the System.ValueTuple structs, which implement the underlying types for C# 7 tuples.

In C# 6.0 after invoking an appropriate Tuple object constructor, the object magically contains a series of properties named Item1, Item2 and so on, one for each type parameter.
There wasn’t an easy way to rename the property names with most significant one.
The best way to accomplish this is to create a custom class that inherits from System.Tuple and then expose the Items properties with significant names.

C# 7.0 comes with a much better Tuple object. First of all it is a value type, then it performs faster (reference type can still be used).

A Tuple variable, known as Tuple literal, containing multiple values each of which may have its own data type:

var axes = ("Axis X", "Axis Y", "Axis Z");
Console.WriteLine("Axes are: {0}, {1} and {2} ", axes.Item1, axes.Item2, axes.Item3);
Console.WriteLine($"Axes are: {axes.Item1}, {axes.Item2}, {axes.Item3}");

Members type are automatically inferred by the compiler, in the above example are string, string, string. Furthermore, it’s possible to override the default named Tuple public properties ItemX

var points = (x: 100, y: 20, z: 50);
Console.WriteLine("Points are : {0} , {1}, {2} ", points.x, points.y, points.z);

And different data types are also possible:

var customerInfo = ("Joe Doe", 10, false); //(string,int,bool)
Console.WriteLine("Customer information: Name: {0}, Discount: {1}, IsProspect: {2} ", customerInfo.Item1, customerInfo.Item2, customerInfo.Item3);

A Tuple member may be a reference type:

var customerInformation = (customer: new Customer { FirstName = "Foo", LastName = "Bar" }, Id: 1 );
Console.WriteLine("Customer details are - Id: {0}, First Name: {1}, Last Name: {2}", customerInformation.Id, customerInformation.customer.FirstName, customerInformation.customer.LastName);

public class Customer
   public string FirstName { get; set; }
   public string LastName { get; set; }

A function may return a Tuple literal:

(string description, double x, double y, double z) GetMultipleValues()
   return ("Short description", 100D, 120D, 140D);

In this case the funcion gets called in this way:

var values = GetMultipleValues();
Console.WriteLine("Values are: string value={0}, first double value={1}, second double value={2}, third double value={3}", values.Item1, values.Item2, values.Item3, values.Item4);

The return statement in the above function may contain the explicit name of each field:

return (description: "Short description", x: 100D, y: 120D, z: 140D);

So in this way the function would get called:

var values = GetMultipleValues();
Console.WriteLine("Values are: string value={0}, first double value={1}, second double value={2}, "third double value={3}", values.description, values.x, values.y, values.z);

435 thoughts on “C# 7.0 – #3. New Tuple value type

  1. Pingback: canadian drugstore
  2. Pingback: cialis from canada
  3. Pingback: buy cialis cheap
  4. Pingback: buy cialis uk
  5. Pingback: buy viagra usa
  6. Pingback: deiun.flazio.com
  7. Pingback: kertyun.flazio.com
  8. Pingback: online pharmacies
  9. Pingback: canadianpharmacy
  10. Pingback: canadian pharmacy
  11. Pingback: northwestpharmacy
  12. Pingback: canadianpharmacy
  13. Pingback: aonubs.website2.me
  14. Pingback: canada pharmacy
  15. Pingback: drugstore online
  16. Pingback: kwersv.proweb.cz
  17. Pingback: kwsde.zombeek.cz
  18. Pingback: canada drugs
  19. Pingback: confeitofilm
  20. Pingback: buy viagra delhi
  21. Pingback: canada pharmacy
  22. Pingback: drugstore online
  23. Pingback: stromectol generic
  24. Pingback: stromectol usa
  25. Pingback: stromectol lice
  26. Pingback: canadianpharmacy
  27. Pingback: canadian pharmacy
  28. Pingback: online drug store
  29. Pingback: canada viagra
  30. Pingback: stromectol mites
  31. Pingback: stromectol rosacea
  32. Pingback: stromectol reviews
  33. Pingback: buying stromectol
  34. Pingback: ivermectina dosis
  35. Pingback: canadian cialis
  36. Pingback: buy viagra 25mg
  37. Pingback: Northwest Pharmacy
  38. Pingback: drugs for sale
  39. Pingback: online pharmacies
  40. Pingback: buy viagra now
  41. Pingback: new drugs for ed
  42. Pingback: erection pills
  43. Pingback: canada pharmacy
  44. Pingback: buy viagra now
  45. Pingback: canadianpharmacy
  46. Pingback: gravatar.comkqwsh
  47. Pingback: pharmacy
  48. Pingback: online pharmacy
  49. Pingback: film
  50. Pingback: 2022-film
  51. Pingback: mangalib
  52. Pingback: online drug store
  53. Pingback: x
  54. Pingback: 9xflix
  55. Pingback: xnxx
  56. Pingback: 123movies
  57. Pingback: kinokrad
  58. Pingback: batmanapollo
  59. Pingback: www.dibiz.comgdooc
  60. Pingback: canada pharmacy
  61. Pingback: vsovezdeisrazu
  62. Pingback: 2023
  63. Pingback: ipsychologos
  64. Pingback: canada medication
  65. Pingback: yug-grib.ru
  66. Pingback: studio-tatuage.ru
  67. Pingback: online pharmacy
  68. Pingback: video.vipspark.ru
  69. Pingback: vitaliy-abdulov.ru
  70. Pingback: psychophysics.ru
  71. Pingback: canadian drug
  72. Pingback: itsmasum.com

Comments are closed.