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

Type checking 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 pattern matching.

C Sharp (C#)
if (obj is [1] number) {
    Console.WriteLine($"Number: {number}");
}
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cbool
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not match the object's actual type.
Forgetting to use pattern matching syntax.
2fill in blank
medium

Complete the code to check if the object is a string and get its length.

C Sharp (C#)
if (obj is [1] text) {
    Console.WriteLine($"Length: {text.Length}");
}
Drag options to blanks, or click blank then click option'
Aobject
Bstring
Cchar
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using char instead of string.
Trying to access Length on a non-string type.
3fill in blank
hard

Fix the error in the pattern matching to check if obj is a double.

C Sharp (C#)
if (obj is [1] value) {
    Console.WriteLine($"Double value: {value}");
}
Drag options to blanks, or click blank then click option'
Afloat
Bdecimal
Cint
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using float which is single precision.
Using decimal which is a different numeric type.
4fill in blank
hard

Fill both blanks to check if obj is a string and print its uppercase form.

C Sharp (C#)
if (obj is [1] text) {
    Console.WriteLine(text.[2]());
}
Drag options to blanks, or click blank then click option'
Astring
Bint
CToUpper
DLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of string.
Using Length instead of ToUpper.
5fill in blank
hard

Fill all three blanks to create a pattern matching that checks if obj is a list of integers and prints the count.

C Sharp (C#)
if (obj is [1] list) {
    Console.WriteLine($"Count: {list.[2]");
    foreach (var item in list) {
        Console.WriteLine(item.[3]);
    }
}
Drag options to blanks, or click blank then click option'
AList<int>
BCount
CToString()
DLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length instead of Count for lists.
Not using generic type syntax List<int>.
Trying to print integers directly without converting to string.