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

Property 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 check if the object has a property Age equal to 30 using a property pattern.

C Sharp (C#)
if (person is { Age: [1] })
{
    Console.WriteLine("Age is 30");
}
Drag options to blanks, or click blank then click option'
A30
BAge
Cperson.Age
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name instead of the value.
Trying to access the property outside the pattern.
2fill in blank
medium

Complete the code to check if the object has a Name property equal to "Alice" using a property pattern.

C Sharp (C#)
if (person is { Name: [1] })
{
    Console.WriteLine("Name is Alice");
}
Drag options to blanks, or click blank then click option'
AName
B"Bob"
CAlice
D"Alice"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string value.
Using the property name instead of the value.
3fill in blank
hard

Fix the error in the property pattern to correctly check if the object has a property IsActive set to true.

C Sharp (C#)
if (user is { IsActive: [1] })
{
    Console.WriteLine("User is active");
}
Drag options to blanks, or click blank then click option'
Atrue
B"true"
CTrue
DisActive
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around boolean values.
Capitalizing boolean values incorrectly.
4fill in blank
hard

Fill both blanks to check if the object has properties Age greater than 18 and Name equal to "John" using property patterns.

C Sharp (C#)
if (person is { Age: [1], Name: [2] })
{
    Console.WriteLine("Adult named John");
}
Drag options to blanks, or click blank then click option'
A> 18
B"John"
D< 18
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect comparison operators.
Not quoting string values.
5fill in blank
hard

Fill all three blanks to create a property pattern that matches an object with Age less than 65, Name starting with 'A', and IsActive true.

C Sharp (C#)
if (employee is { Age: [1], Name: [2], IsActive: [3] })
{
    Console.WriteLine("Active employee with name starting with A and age under 65");
}
Drag options to blanks, or click blank then click option'
A< 65
Btrue
Cvar n when n.StartsWith("A")
D> 65
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect operators for age comparison.
Not using var and when for string pattern.
Misplacing boolean value syntax.