Are C# anonymous types redundant in C# 7

后端 未结 2 581
天命终不由人
天命终不由人 2021-02-19 03:44

Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples?

For example, the following line

collection.S         


        
2条回答
  •  伪装坚强ぢ
    2021-02-19 04:09

    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:

    • C# 7 tuples are ValueTuple<>s. That means they are value types while anonymous types are reference types.
    • Tuples allow static typing at compile time since they are a type that can be expressed explicitly. As such, you can use them as method arguments, return types, etc.
    • Members of an anonymous type are actual properties that exist on the type. Tuple items are fields.
    • The properties of an anonymous type have an actual name, while the fields on a tuple are just named 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.
    • Because creating an anonymous type actually creates a type under the hood, you have a level of type safety with them. Since tuples are just generic containers with applied type arguments, you do not have full type safety with them. For example an (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.
    • As Jon Skeet mentioned, the C# 7 tuple syntax is currently not supported in expression trees.

提交回复
热议问题