C#.NET constructor order — if class B inherits A, which constructor runs first when creating a B object?

Difficulty: Easy

Correct Answer: A then B

Explanation:

Introduction / Context:Understanding constructor order is vital for correct initialization in inheritance hierarchies. Base initialization must complete before derived initialization begins.

Given Data / Assumptions:

  • Class B derives from class A.
  • No special cases like struct types are involved.

Concept / Approach:C# guarantees that when constructing a derived object, the base constructor runs first. This ensures that base state is initialized before derived code uses it. If a specific base constructor is required, the derived constructor must chain to it using : base(...).

Step-by-Step Solution:

Create new B(...) → runtime first invokes A’s constructor.After A finishes, B’s constructor body executes.Therefore the order is A then B.

Verification / Alternative check:Add Console writes in each constructor to see the order at runtime.

Why Other Options Are Wrong:(a) reverses the mandated order; (c) and (d) ignore the chain; (e) access modifiers don’t alter order (they can affect accessibility, not sequence).

Common Pitfalls:Thinking derived constructors can run before base — they cannot.

Final Answer:A then B

More Questions from Inheritance

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion