C# 7:How can I deconstruct an object into a single value using a tuple?

后端 未结 2 2019
Happy的楠姐
Happy的楠姐 2021-01-12 02:52

One of the nice new features of C# 7 is the possibility to define deconstructors for classes and assign the deconstructed values directly to a value tuple.

However,

2条回答
  •  忘掉有多难
    2021-01-12 03:28

    Deconstructions into a single element are not supported in C# 7.0.

    It is unclear why you would need such a mechanism, as you can simply access a property or write a conversion operator to achieve the same thing.

    Conceptually, a tuple of one element is just that one element (you don't need a tuple to hold it). So there is no tuple syntax (using parentheses notation) to facilitate that (not to mention it would be syntactically ambiguous). The same applies for deconstructions.

    Here are the most relevant LDM notes I could find: 2017-03-15 (zero and one element tuples and deconstructions).

    It is possible that such deconstruction could become allowed in some future recursive pattern scenarios, but that has not been finalized yet.

提交回复
热议问题