One of the nice new features in C # 7 is the ability to define deconstructors for classes and assign deconstructed values ββdirectly to the value tuple.
However, if the object is deconstructed into a single value, I cannot find a way to assign it to a tuple. Although there is a type for single-element tuples ( ValueTuple<T> ), shorthand syntax using parentheses does not work here. The only way I found access to the deconstructor was to directly call the Deconstruct method, but this takes away its advantage, since I can use any method for this purpose.
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;
source share