Bird
0
0

Find the issue in this code:

medium📝 Debug Q7 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
Find the issue in this code:
object obj = "test";
if (obj is string s)
{
    Console.WriteLine(s.Length);
}
else
{
    Console.WriteLine("Not a string");
}
AVariable 's' is not declared properly
BNo issue; code runs correctly
CIncorrect use of 'is' operator
DMissing cast before accessing Length
Step-by-Step Solution
Solution:
  1. Step 1: Understand pattern matching with 'is'

    The code uses 'is' with pattern matching to declare 's' if obj is string.
  2. Step 2: Verify code correctness

    Since obj is "test", s is assigned and accessing s.Length is safe. No errors present.
  3. Final Answer:

    No issue; code runs correctly -> Option B
  4. Quick Check:

    'is' pattern matching declares variable safely [OK]
Quick Trick: Use 'is Type var' to declare and check type safely [OK]
Common Mistakes:
MISTAKES
  • Thinking 'is' can't declare variables
  • Expecting cast before member access
  • Confusing 'is' with 'as'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes