Docs: Quadrant switch expression, deconstruct example

Created on 7 Sep 2019  Â·  3Comments  Â·  Source: dotnet/docs

Maybe I'm misreading this, but when accessing Point in:

static Quadrant GetQuadrant(Point point)

We are doing it implicitly but with prior knowledge of what we can deconstruct.

Would it not be more explicit just to access each Point member (x and y which are public) and send them in?

static Quadrant GetQuadrant(int x. Int y)

I don't see what deconstruct helps here with rather it just helps introduce an unnecessary level of indirection.

But perhaps I'm missing the "Point"
Might seem like a thankless task creating new language features... Some of the others are great though, especially the async streaming stuff, keep up the good work. :)


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide Technology - C# What's New P2 Pri2 doc-bug dotnet-csharprod

All 3 comments

Thanks for writing this. I agree the sample could be more clear, and the justification could be easier to see.

The advantage to using a Deconstruct method is that you can reduce the number of overloads for all public methods that have a Point parameter. My having a deconstruction, you need only write the Deconstruct method that takes (x, y). You don't need to write the additional overload that takes a Point.

Another example would make the advantages more clear. It's not always needed, but there are times when it's very useful.

I believe you meant: "By having a deconstruction..."

The deconstruct example is actually not even valid C# code. It would be good to provide a better example or at least valid code.

My compiler throws:

CS8510  The pattern has already been handled by a previous arm of the switch expression.

The problem is the last line

 _ => Quadrant.Unknown

of

static Quadrant GetQuadrant(Point point) => point switch
{
    (0, 0) => Quadrant.Origin,
    var (x, y) when x > 0 && y > 0 => Quadrant.One,
    var (x, y) when x < 0 && y > 0 => Quadrant.Two,
    var (x, y) when x < 0 && y < 0 => Quadrant.Three,
    var (x, y) when x > 0 && y < 0 => Quadrant.Four,
    var (_, _) => Quadrant.OnBorder,
    _ => Quadrant.Unknown
};

The previous switch statements just capture all the possibilities -- or am I missing something? It's just confusing. I'm new to that feature and when I see even the documentation sample wrong, I start to question my understanding of the whole thing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stanuku picture stanuku  Â·  3Comments

ite-klass picture ite-klass  Â·  3Comments

stjepan picture stjepan  Â·  3Comments

mekomlusa picture mekomlusa  Â·  3Comments

FrancescoBonizzi picture FrancescoBonizzi  Â·  3Comments