Bird
Raised Fist0

Consider the class below:

hard🚀 Application Q8 of Q15
C Sharp (C#) - Classes and Objects
Consider the class below:
class Employee {
  public string Name;
  public Employee(string name) {
    this.Name = name;
  }
  public Employee() : this("No Name") {}
}

What is the effect of the parameterless constructor in this class?
AIt causes a compile-time error due to constructor chaining
BIt leaves <code>Name</code> uninitialized
CIt initializes <code>Name</code> to "No Name" by calling the other constructor
DIt sets <code>Name</code> to null explicitly
Step-by-Step Solution
Solution:
  1. Step 1: Understand constructor chaining

    The parameterless constructor calls the parameterized constructor with "No Name".
  2. Step 2: Effect on field

    The Name field is set to "No Name" via the chained constructor.
  3. Final Answer:

    It initializes Name to "No Name" by calling the other constructor -> Option C
  4. Quick Check:

    Constructor chaining sets default value [OK]
Quick Trick: Parameterless constructor calls other with default value [OK]
Common Mistakes:
MISTAKES
  • Assuming Name remains null
  • Thinking constructor chaining causes error
  • Ignoring the call to the other constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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