0
0
C Sharp (C#)programming~10 mins

Positional patterns in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to match a point with coordinates (0, 0).

C Sharp (C#)
if (point is ([1], 0))
{
    Console.WriteLine("Point is on the Y axis.");
}
Drag options to blanks, or click blank then click option'
A1
B0
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a literal 0.
Using 1 instead of 0.
2fill in blank
medium

Complete the code to extract the X coordinate from a point using positional patterns.

C Sharp (C#)
if (point is ([1], int y))
{
    Console.WriteLine($"X coordinate is {x}");
}
Drag options to blanks, or click blank then click option'
Ax
Bvar x
C0
Dint x
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'x' without type.
Using 'var x' which is invalid in positional patterns.
3fill in blank
hard

Fix the error in the positional pattern to correctly match a point with X > 0.

C Sharp (C#)
if (point is (int x, int y) && x [1] 0)
{
    Console.WriteLine("X is positive.");
}
Drag options to blanks, or click blank then click option'
A>
B<=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which checks for negative values.
Using '==' which checks for equality.
4fill in blank
hard

Fill both blanks to match a point where X equals Y.

C Sharp (C#)
if (point is ([1] x, [2] y) && x == y)
{
    Console.WriteLine("X equals Y.");
}
Drag options to blanks, or click blank then click option'
Aint
Bvar
Cdouble
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing types like int and double.
Using 'var' which is not allowed in positional patterns.
5fill in blank
hard

Fill all three blanks to create a method that uses positional patterns to check if a point is at the origin.

C Sharp (C#)
bool IsOrigin((int x, int y) point) => point is ([1], [2]);

// Usage
var p = (0, 0);
Console.WriteLine(IsOrigin(p)); // [3]
Drag options to blanks, or click blank then click option'
A0
Btrue
Cfalse
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-zero values in the pattern.
Returning false instead of true for the origin.