Bird
0
0

Which of the following is the correct syntax to check if obj is an int and assign it to number?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
Which of the following is the correct syntax to check if obj is an int and assign it to number?
Aif (obj is int number) { }
Bif (obj as int number) { }
Cif (obj == int number) { }
Dif (obj instanceof int number) { }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct C# type pattern syntax

    The correct syntax uses is Type variable to check and assign.
  2. Step 2: Compare options

    if (obj is int number) { } uses is int number, which is valid. Others use invalid keywords or syntax.
  3. Final Answer:

    if (obj is int number) { } -> Option A
  4. Quick Check:

    is Type var syntax is correct [OK]
Quick Trick: Use is Type var for type check and assignment [OK]
Common Mistakes:
MISTAKES
  • Using 'as' instead of 'is' for type checking
  • Using '==' to compare types
  • Using JavaScript or Java keywords like 'instanceof'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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