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

Casting with as and is operators 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 cast the object to a string using the 'as' operator.

C Sharp (C#)
object obj = "hello";
string str = obj [1] string;
Drag options to blanks, or click blank then click option'
Ato
Bis
Ccast
Das
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'is' instead of 'as' for casting.
Trying to use 'cast' keyword which does not exist.
2fill in blank
medium

Complete the code to check if 'obj' is a string using the 'is' operator.

C Sharp (C#)
object obj = "world";
if (obj [1] string)
{
    Console.WriteLine("It's a string!");
}
Drag options to blanks, or click blank then click option'
Aas
Bis
C==
Dequals
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' for type checking.
Using '==' which compares references, not types.
3fill in blank
hard

Fix the error in the code by replacing the incorrect cast with the correct 'as' operator.

C Sharp (C#)
object obj = 123;
string str = obj [1] string;
if (str != null)
{
    Console.WriteLine(str);
}
Drag options to blanks, or click blank then click option'
Aas
B(string)
Cis
Dto
Attempts:
3 left
💡 Hint
Common Mistakes
Using explicit cast '(string)' which can cause exceptions.
Using 'is' which only checks type but does not cast.
4fill in blank
hard

Fill both blanks to safely cast 'obj' to string and check if the cast succeeded.

C Sharp (C#)
object obj = GetObject();
string str = obj [1] string;
if (str [2] null)
{
    Console.WriteLine(str);
}
Drag options to blanks, or click blank then click option'
Aas
B==
C!=
Dis
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'is' instead of 'as' for casting.
Checking with '==' null instead of '!=' null.
5fill in blank
hard

Fill all three blanks to check type with 'is', cast with 'as', and verify the cast result.

C Sharp (C#)
object obj = GetObject();
if (obj [1] string)
{
    string str = obj [2] string;
    if (str [3] null)
    {
        Console.WriteLine(str);
    }
}
Drag options to blanks, or click blank then click option'
Ais
Bas
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' null instead of '!=' null.
Using explicit cast instead of 'as'.