Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the as operator do in C#?
The as operator tries to cast an object to a specified type. If it fails, it returns null instead of throwing an error.
Click to reveal answer
beginner
How does the is operator work in C#?
The is operator checks if an object is of a certain type and returns true or false.
Click to reveal answer
beginner
What happens if you use as to cast an incompatible type?
Instead of causing an error, as returns null when the cast is not possible.
Click to reveal answer
intermediate
Can is operator be used to safely cast an object?
No, is only checks the type and returns a boolean. To cast safely, combine is with a cast or use as.
Click to reveal answer
beginner
Show a simple example of using as and is operators.
Example:
object obj = "hello";
string s1 = obj as string; // s1 = "hello"
if (obj is string s2) {
// s2 is "hello"
}
Click to reveal answer
What does the as operator return if the cast fails?
Azero
Bthrows an exception
Cfalse
Dnull
✗ Incorrect
The as operator returns null if the cast is not possible, avoiding exceptions.
Which operator checks if an object is a certain type and returns a boolean?
Ais
Bas
Ccast
Dtypeof
✗ Incorrect
The is operator returns true or false depending on the object's type.
What is the result of obj as string if obj is an integer?
Anull
Binteger value
Cthrows exception
Dtrue
✗ Incorrect
Casting an integer to string with as returns null because the types are incompatible.
How can you safely cast an object after checking its type with is?
AUse <code>as</code> operator
BUse <code>typeof</code>
CUse explicit cast after <code>is</code> check
DUse <code>new</code> keyword
✗ Incorrect
After confirming type with is, you can safely cast using explicit cast syntax.
Which operator is better to avoid exceptions when casting?
Ais
Bas
Cexplicit cast
Dtypeof
✗ Incorrect
The as operator returns null instead of throwing exceptions, making it safer.
Explain how the as and is operators help with type casting in C#.
Think about how to safely convert or check types without crashing your program.
You got /3 concepts.
Describe a scenario where using as is better than an explicit cast.
Consider when your program needs to keep running even if the cast is not possible.
You got /3 concepts.
Practice
(1/5)
1. What does the as operator do in C#?
easy
A. It tries to cast an object to a type and returns null if it fails.
B. It checks if an object is exactly a certain type and returns true or false.
C. It converts a value type to a string representation.
D. It throws an exception if the cast is invalid.
Solution
Step 1: Understand the as operator behavior
The as operator attempts to cast an object to a specified type but returns null instead of throwing an exception if the cast fails.
Step 2: Compare with other options
It checks if an object is exactly a certain type and returns true or false. describes the is operator, It converts a value type to a string representation. is unrelated, and It throws an exception if the cast is invalid. is incorrect because as does not throw exceptions.
Final Answer:
It tries to cast an object to a type and returns null if it fails. -> Option A
Quick Check:
as returns null on failure [OK]
Hint: Remember: as returns null, no exceptions [OK]
Common Mistakes:
Confusing as with is
Thinking as throws exceptions
Assuming as works with value types
2. Which of the following is the correct syntax to check if an object obj is of type string using the is operator?
easy
A. if (obj == string) { /* code */ }
B. if (obj as string) { /* code */ }
C. if (obj is (string)) { /* code */ }
D. if (obj is string) { /* code */ }
Solution
Step 1: Recall is operator syntax
The correct syntax to check type is if (obj is Type), so if (obj is string) is valid.
Step 2: Analyze other options
if (obj as string) { /* code */ } uses as incorrectly in an if condition, if (obj == string) { /* code */ } compares object to type which is invalid, if (obj is (string)) { /* code */ } has unnecessary parentheses.
Final Answer:
if (obj is string) { /* code */ } -> Option D
Quick Check:
is syntax: obj is Type [OK]
Hint: Use is like: if (obj is Type) [OK]
Common Mistakes:
Using as in if condition directly
Comparing object to type with ==
Adding unnecessary parentheses in is check
3. What is the output of the following code?
object obj = "hello";
string s = obj as string;
Console.WriteLine(s != null ? s.ToUpper() : "null");
medium
A. HELLO
B. null
C. hello
D. Runtime error
Solution
Step 1: Analyze the as cast
The object obj holds a string "hello". Using as string casts it successfully, so s is "hello".
Step 2: Evaluate the conditional output
Since s is not null, s.ToUpper() is called, producing "HELLO".
Final Answer:
HELLO -> Option A
Quick Check:
as cast success means uppercase output [OK]
Hint: If as cast succeeds, use the object; else null [OK]
Common Mistakes:
Assuming as throws exception on failure
Forgetting to check for null after as
Confusing output case sensitivity
4. Identify the error in this code snippet:
object obj = 123;
string s = obj as string;
Console.WriteLine(s.Length);
medium
A. The is operator is used incorrectly.
B. The as cast will fail and s will be null, causing a null reference exception.
C. You cannot use as with value types like int.
D. The code will compile but print 3.
Solution
Step 1: Understand as cast with incompatible types
Since obj holds an int (123), casting it as string returns null.
Step 2: Analyze the usage
Since s is null, calling s.Length causes a null reference exception.
Final Answer:
The as cast will fail and s will be null, causing a null reference exception. -> Option B
Quick Check:
as returns null on failure; check before use [OK]
Hint: Always check for null after as cast before use [OK]
Common Mistakes:
Assuming is fixes null issues
Not checking s for null before accessing members
Thinking as works with value types
5. Given the classes:
class Animal { }
class Dog : Animal { public string Bark() => "Woof!"; }
What is the safest way to call Bark() on an Animal reference a that might be a Dog?
hard
A. if (a is Dog) { Console.WriteLine(a.Bark()); }
B. Console.WriteLine(((Dog)a).Bark());
C. Dog d = a as Dog; if (d != null) Console.WriteLine(d.Bark());
D. Console.WriteLine(a.Bark());
Solution
Step 1: Understand safe casting with as
Using as casts a to Dog safely, returning null if a is not a Dog.
Step 2: Check for null before calling Bark()
Checking d != null ensures Bark() is called only if a is a Dog, avoiding exceptions.
Step 3: Compare with other options
if (a is Dog) { Console.WriteLine(a.Bark()); } checks type but doesn't cast, so a.Bark() is invalid because Animal has no Bark(). Console.WriteLine(((Dog)a).Bark()); casts without check, risking exceptions. Console.WriteLine(a.Bark()); is invalid because Animal has no Bark().
Final Answer:
Dog d = a as Dog; if (d != null) Console.WriteLine(d.Bark()); -> Option C
Quick Check:
Use as + null check for safe cast [OK]
Hint: Use as then check null before method call [OK]