Dec 8, 2019 in Dot Net
Q: Positional patterns in C#

1 Answer

0 votes
Dec 8, 2019

Some types include a Deconstruct method that deconstructs its properties into discrete variables. When a Deconstruct method is accessible, you can use positional patterns to inspect properties of the object and use those properties for a pattern. Consider the following Point class that includes a Deconstruct method to create discrete variables for X and Y:

public class Point


    public int X { get; }

    public int Y { get; }

    public Point(int x, int y) => (X, Y) = (x, y);

    public void Deconstruct(out int x, out int y) =>

        (x, y) = (X, Y);


Additionally, consider the following enum that represents various positions of a quadrant:

public enum Quadrant










he following method uses the positional pattern to extract the values of x and y. Then, it uses a when clause to determine the Quadrant of the point:

Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

0 votes
0 votes
0 votes
Dec 8, 2019 in Dot Net