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

Type 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 is an integer using a type pattern.

C Sharp (C#)
if (obj is [1] number) {
    Console.WriteLine($"Number: {number}");
}
Drag options to blanks, or click blank then click option'
Astring
Bint
Cbool
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong type like 'string' instead of 'int'.
Forgetting to declare the variable after the type.
2fill in blank
medium

Complete the code to check if the object is a string and has length greater than 5.

C Sharp (C#)
if (obj is [1] text && text.Length > 5) {
    Console.WriteLine($"Long text: {text}");
}
Drag options to blanks, or click blank then click option'
Aint
Bdouble
Cstring
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'string' for text.
Not declaring the variable after the type.
3fill in blank
hard

Fix the error in the type pattern to correctly check for a double value.

C Sharp (C#)
if (obj is [1] value) {
    Console.WriteLine($"Double value: {value}");
}
Drag options to blanks, or click blank then click option'
Adouble
Bint
Cstring
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'double'.
Using 'string' or 'bool' for numeric checks.
4fill in blank
hard

Fill both blanks to create a type pattern that checks if obj is a bool and true.

C Sharp (C#)
if (obj is [1] flag && flag [2] true) {
    Console.WriteLine("Flag is true");
}
Drag options to blanks, or click blank then click option'
Abool
B==
C!=
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'bool' for the type.
Using '!=' instead of '==' for comparison.
5fill in blank
hard

Fill both blanks to create a type pattern that checks if obj is a string with length less than 4 and prints it.

C Sharp (C#)
if (obj is [1] text && text.Length [2] 4) {
    Console.WriteLine($"Short text: {text}");
}
Drag options to blanks, or click blank then click option'
Aint
B<
Cstring
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'string' for the type.
Using '>' instead of '<' for the length comparison.