C# Error CS8132 – Cannot deconstruct a tuple of ‘{0}’ elements into ‘{1}’ variables.

C# Error

CS8132 – Cannot deconstruct a tuple of ‘{0}’ elements into ‘{1}’ variables.

Reason for the Error & Solution

Cannot deconstruct a tuple of elements into variables.

Example

The following sample generates CS8132:

// CS8132.cs (5,9)
class Program
{
    static void F(object x, object y, object? z)
    {
        (object? a, object? b) = (x, y, z = 3);
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(z);
    }
}

To correct this error

To deconstruct to a tuple with a smaller number of elements, using the discard variables will correct this error:

class Program
{
    static void F(object x, object y, object? z)
    {
        (object? a, object? b, object _) = (x, y, z = 3);
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(z);
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...