Search code examples
c#.netcompiler-errorskey-valuec#-7.0

Where's Deconstruct method of KeyValuePair<> struct?


I'm sure that previously I saw this method, because one year ago I've asked: What is the purpose of Deconstruct method in KeyValuePair<> struct?

But now I simply can't find it, or any trace about it's removal, any questions, nothing.

Compiler agrees:

var s = new KeyValuePair<int, int>(1, 3);
var (x, y) = s;

Error CS1061 'KeyValuePair' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair' could be found (are you missing a using directive or an assembly reference?

Error CS8129 No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair', with 2 out parameters and a void return type.

What's going on?


Solution

  • You might be running your code on .NET Framework. Whilst the C# 7 deconstruction syntax is supported in both .NET Framework and .NET Core, the Deconstruct method for KeyValuePair<TKey,TValue> is currently only supported in .NET Core 2.0 and later. You can check the "Applies to" section on Microsoft Docs.