Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples?
For example, the following line
collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray();
makes the following line
collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray();
redundant.
What would be the use case where one is better used over the other (for either performance reasons or optimization)?
Obviously, if there is a need for more than six fields, tuples cannot be used, but is there something a bit more nuanced to it?
There are various differences between anonymous types and C# 7 tuples, which may or may not make one more appropriate than the other in certain situations:
ValueTuple<>
s. That means they are value types while anonymous types are reference types.ItemN
(for numbers N
). The labels are just metadata information that is mostly used by the compiler, and is not persisted with the actual tuple object.(int, int)
tuple for a size would be fully compatible to an (int, int)
tuple for a position, while anonymous types are closed off completely.