diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index 02515002c22b1..164e59af97fc6 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -1,7 +1,7 @@ --- title: Deconstructing tuples and other types description: Learn how to deconstruct tuples and other types. -ms.date: 03/22/2021 +ms.date: 06/30/2021 --- # Deconstructing tuples and other types @@ -41,13 +41,15 @@ There are three ways to deconstruct a tuple: :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple5.cs" ID="Snippet1"::: +- Beginning in C# 10, you can mix variable declaration and assignment in a deconstruction. + + :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple6.cs" ID="Snippet1"::: + You can't specify a specific type outside the parentheses even if every field in the tuple has the same type. Doing so generates compiler error CS8136, "Deconstruction 'var (...)' form disallows a specific type for 'var'.". You must assign each element of the tuple to a variable. If you omit any elements, the compiler generates error CS8132, "Can't deconstruct a tuple of 'x' elements into 'y' variables." -You can't mix declarations and assignments to existing variables on the left-hand side of a deconstruction. The compiler generates error CS8184, "a deconstruction can't mix declarations and expressions on the left-hand-side." when the members include newly declared and existing variables. - ## Tuple elements with discards Often when deconstructing a tuple, you're interested in the values of only some elements. Starting with C# 7.0, you can take advantage of C#'s support for *discards*, which are write-only variables whose values you've chosen to ignore. A discard is chosen by an underscore character ("\_") in an assignment. You can discard as many values as you like; all are represented by the single discard, `_`. diff --git a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple6.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple6.cs new file mode 100644 index 0000000000000..18427f99eb674 --- /dev/null +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple6.cs @@ -0,0 +1,22 @@ +public class Example5 +{ + // + public static void Main() + { + string city = "Raleigh"; + int population = 458880; + + (city, population, double area) = QueryCityData("New York City"); + + // Do something with the data. + } + // + + private static (string, int, double) QueryCityData(string name) + { + if (name == "New York City") + return (name, 8175133, 468.48); + + return ("", 0, 0); + } +} diff --git a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj index 5b064a3fa2f37..a8f0c48be64a4 100644 --- a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 enable deconstruction.Program