C# 7 Nested Deconstruction

CSharp · Console

/Program.cs

using System;

public class Program
{
    public static void Main()
    {

        var box = new Box(20, 50);

        var (width, height, (innerWidth, innerHeight)) = box; // deconstruction
        Console.WriteLine($"內盒寬高 = {innerWidth} x {innerHeight}");

    }
}

public class Box
{
    public int Width { get; }
    public int Height { get; }

    public Box(int width, int height)
    {
        Width = width;
        Height = height;
    }
   
    public void Deconstruct(out int width, out int height)
    {
        width = this.Width;
        height = this.Height;
    }

    public void Deconstruct(out int width, out int height, out Box box)
    {
        width = this.Width;
        height = this.Height;
        box = new Box(width / 2, height / 2); // 內盒是外盒的一半大小
    }
}

;