简体   繁体   English

C# 7:如何使用元组将对象解构为单个值?

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

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. C# 7 的一个不错的新特性是可以为类定义解构器并将解构后的值直接分配给值元组。

However, in the case that the object is deconstructed into a single value, I can't find a way to assign it to a tuple.但是,在对象被解构为单个值的情况下,我找不到将其分配给元组的方法。 Although there is a type for tuples with a single element ( ValueTuple<T> ), the shorthand syntax using parentheses doesn't work here.尽管有一种用于具有单个元素 ( ValueTuple<T> ) 的元组的类型,但使用括号的速记语法在这里不起作用。 The only way I found to access the deconstructor was to call the Deconstruct method directly, but this eliminates its benefit, as I could use any method for this end.我发现访问解构器的唯一方法是直接调用Deconstruct方法,但这消除了它的好处,因为我可以为此使用任何方法。

Does anyone know a better way to deconstruct an object into a single value?有谁知道将对象解构为单个值的更好方法?

Here is my test code:这是我的测试代码:

class TestClass
{
    private string s;
    private int n;
    public TestClass(string s, int n) => (this.s, this.n) = (s, n);
    public void Deconstruct(out string s) => s = this.s;
    public void Deconstruct(out string s, out int n) => (s, n) = (this.s, this.n);
}

static void Main(string[] args)
{
    var testObject = new TestClass("abc", 3);

    var (s1) = testObject; // sytax error (comma expected)
    ValueTuple<string> t = testObject; // error: "no implicit conversion from TestClass to (string)"
    testObject.Deconstruct(out string s2); // this works
    var (s3, n) = testObject; // no problem

    Console.WriteLine($"{s1} {t.Item1} {s2} {s3} {n}");
    Console.ReadKey();
}

Although there is a type for tuples with a single element ( ValueTuple<T> ), the shorthand syntax using parentheses doesn't work here.尽管有一种用于具有单个元素 ( ValueTuple<T> ) 的元组的类型,但使用括号的速记语法在这里不起作用。

That's correct.这是正确的。 The tuple syntax only works for tuples of 2 values or more, so the Deconstruct method with only one out parameter is not very useful.元组语法仅适用于 2 个或更多值的元组,因此只有一个out参数的Deconstruct方法不是很有用。 (There is even a ValueTuple type for 0 elements) (甚至还有一个ValueTuple类型为 0 个元素)

The shortest solution is to just ignore the 2nd parameter:最短的解决方案是忽略第二个参数:

var (s1, _) = testObject;

Edit: based on comments, a little clarification.编辑:根据评论,稍微澄清一下。
As of C# 7, _ is no longer a variable in this situation.从 C# 7 开始,在这种情况下_不再是变量。 It is a new feature called 'discard'.这是一项称为“丢弃”的新功能。
Even if you have multiple out parameters (and even if they are different types) you can ignore any of them with an underscore:即使您有多个输出参数(即使它们是不同的类型),您也可以使用下划线忽略它们中的任何一个:

var (s1, _, _, _) = testObject;

Deconstructions into a single element are not supported in C# 7.0. 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).以下是我能找到的最相关的 LDM 笔记: 2017-03-15 (零和一元素元组和解构)。

It is possible that such deconstruction could become allowed in some future recursive pattern scenarios, but that has not been finalized yet.在某些未来的递归模式场景中可能会允许这种解构,但这还没有最终确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM