Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Properties and Encapsulation
Identify the error in this code snippet:
class User {
  public string Username { get; set; }
}

var user = new User();
user.Username = 12345;
Console.WriteLine(user.Username);
AMissing get accessor in property
BCannot assign int to string property
CUsername property must be readonly
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check property type

    Username is a string property.
  2. Step 2: Check assigned value type

    Assigning integer 12345 to a string property causes a type mismatch error.
  3. Final Answer:

    Cannot assign int to string property -> Option B
  4. Quick Check:

    Type mismatch causes error [OK]
Quick Trick: Match property type with assigned value type [OK]
Common Mistakes:
MISTAKES
  • Assigning wrong data type to property
  • Ignoring type mismatch errors
  • Assuming implicit conversion works

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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