Bird
0
0

Identify the error in this C# class constructor and how to fix it:

medium📝 Debug Q14 of 15
C Sharp (C#) - Classes and Objects
Identify the error in this C# class constructor and how to fix it:
class Book {
  public string Title;
  public Book(string title) {
    title = Title;
  }
}
ATitle should be private, not public
BConstructor name should be lowercase book
CMissing return type void in constructor
DThe assignment is reversed; should be Title = title;
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the assignment inside constructor

    The code assigns title = Title, which sets the parameter to the field's value, not the other way around.
  2. Step 2: Correct the assignment direction

    It should assign the field Title to the parameter value: Title = title; to initialize properly.
  3. Final Answer:

    The assignment is reversed; should be Title = title; -> Option D
  4. Quick Check:

    Field = parameter to initialize correctly [OK]
Quick Trick: Assign field = parameter inside constructor [OK]
Common Mistakes:
MISTAKES
  • Reversing assignment direction
  • Changing constructor name incorrectly
  • Adding return type to constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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