Complete the code to cast the object to a string using the 'as' operator.
object obj = "hello"; string str = obj [1] string;
The 'as' operator attempts to cast an object to a specified type and returns null if it fails.
Complete the code to check if 'obj' is a string using the 'is' operator.
object obj = "world"; if (obj [1] string) { Console.WriteLine("It's a string!"); }
The 'is' operator checks if an object is of a certain type and returns a boolean.
Fix the error in the code by replacing the incorrect cast with the correct 'as' operator.
object obj = 123; string str = obj [1] string; if (str != null) { Console.WriteLine(str); }
The '(string)' cast throws an exception if the cast fails. Using 'as' returns null instead, which is safer here.
Fill both blanks to safely cast 'obj' to string and check if the cast succeeded.
object obj = GetObject(); string str = obj [1] string; if (str [2] null) { Console.WriteLine(str); }
Use 'as' to cast safely and '!=' to check if the cast was successful (not null).
Fill all three blanks to check type with 'is', cast with 'as', and verify the cast result.
object obj = GetObject(); if (obj [1] string) { string str = obj [2] string; if (str [3] null) { Console.WriteLine(str); } }
First check type with 'is', then cast with 'as', and finally check that the cast result is not null.