Search code examples
c#tuplesanonymous-typesc#-7.0

C# 7.0 ValueTuples vs Anonymous Types


Looking at the new C# 7.0 ValueTuples, I am wondering if they will completely replace Anonymous Types. I understand that ValueTuples are structs and therefore behave a bit differently than Anonymous Types which are classes. I don't see a use-case, however, in which I would prefer using an Anonymous Type over a ValueTuple.

Are there any use-cases where using an Anonymous Type would still be beneficial over using ValueTuples in C# 7.0?


Solution

  • Anonymous types are immutable, ValueTuples are not. This is reflected in the fact that anonymous types expose properties, ValueTuples expose fields. Data binding almost always requires properties.

    Plenty of existing code only works with reference types, not with value types. What in particular comes to mind are projections in Entity Framework: projections to value types are simply not implemented.