C#.NET — Which statement is correct regarding constructors and destructors?

Difficulty: Easy

Correct Answer: A constructor can be used to set default values and limit instantiation.

Explanation:


Introduction / Context:
Constructors initialize objects and can enforce design constraints. Destructors (finalizers) in C# are limited and differ from C++ semantics. This question asks you to identify the accurate statement among common misconceptions.



Given Data / Assumptions:

  • We consider standard C# behavior (no unsafe or special tooling).
  • “Limit instantiation” includes patterns like private constructors and factory methods.


Concept / Approach:
Constructors can assign default values to fields and may be made private to restrict creation (e.g., singleton, factory-only). C# does not auto-generate copy constructors; developers can write a constructor that copies fields, but it is not provided by the language automatically. Destructors (finalizers) apply only to classes and there can be at most one per class (no parameters, non-overloadable).



Step-by-Step Solution:

Option A: True — constructors both initialize and can restrict via access modifiers.Option B: False — no automatic copy constructor exists in C#.Option C: False — structures (value types) cannot have destructors.Option D: False — a class may define at most one destructor (finalizer) with fixed signature.


Verification / Alternative check:
Inspect C# specification on constructors and finalizers; attempt to add multiple destructors or a struct destructor to observe compiler errors.



Why Other Options Are Wrong:

  • B: Confuses C# with C++ semantics.
  • C: Structs cannot declare destructors.
  • D: Only one destructor allowed and it has no parameters.


Common Pitfalls:
Assuming finalizers behave like C++ destructors or that copy constructors are auto-generated in C#.



Final Answer:
A constructor can be used to set default values and limit instantiation.

Discussion & Comments

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