What Are the Types of Constructors in C#?

๐Ÿ’ก Concept: Constructor Types

Constructors in C# come in various forms such as default, parameterized, static, private, and copy constructors. Each type serves different purposes in object instantiation and initialization.

๐Ÿ“˜ Quick Intro

Understanding constructor types is key to managing object lifecycle and setting initial values. This allows greater flexibility, encapsulation, and robustness in your class design.

๐Ÿง  Analogy

Imagine buying furniture. A default constructor is like a pre-assembled piece, parameterized constructor is like a customized version based on your preferences, and a static constructor is like a one-time store setup that runs before the first customer arrives.

๐Ÿ”ง Technical Explanation

  • ๐Ÿ›  Default Constructor: No parameters, initializes default values.
  • โš™๏ธ Parameterized Constructor: Takes arguments to initialize fields.
  • ๐Ÿ“ฆ Static Constructor: Initializes static members, runs once per type.
  • ๐Ÿ”’ Private Constructor: Used to prevent instantiation (e.g., Singleton).
  • ๐Ÿ“‹ Copy Constructor: Creates a new object by copying another objectโ€™s state (not built-in, manually created).

๐ŸŽฏ Use Cases

  • ๐Ÿงฑ Default constructor for basic object setup.
  • ๐Ÿ“ค Parameterized for flexible and readable object initialization.
  • โšก Static constructor for static resource setup.
  • ๐Ÿ”’ Private constructor to enforce Singleton or factory patterns.
  • ๐Ÿ“‹ Copy constructor for duplication of objects with same state.

๐Ÿ’ป Code Example

public class Product {
    public string Name;
    public decimal Price;

    // Default Constructor
    public Product() {
        Name = "Default";
        Price = 0;
    }

    // Parameterized Constructor
    public Product(string name, decimal price) {
        Name = name;
        Price = price;
    }

    // Copy Constructor
    public Product(Product p) {
        Name = p.Name;
        Price = p.Price;
    }

    // Static Constructor
    static Product() {
        Console.WriteLine("Static Constructor Initialized");
    }
}

โ“ Interview Q&A

Q1: Name different types of constructors in C#.
A: Default, Parameterized, Static, Private, and Copy constructors.

Q2: What is a static constructor?
A: A constructor that initializes static fields and runs once per type.

Q3: Can a class have multiple constructors?
A: Yes, through constructor overloading.

Q4: What is the use of a private constructor?
A: To restrict object creation and control instantiation.

Q5: What is a copy constructor?
A: A constructor that creates a new object by copying an existing one.

Q6: Are static constructors inherited?
A: No, they are not inherited.

Q7: Can a static constructor be overloaded?
A: No, static constructors cannot take parameters or be overloaded.

Q8: Is it mandatory to define a constructor in every class?
A: No, if none is defined, C# provides a default one automatically.

Q9: When is the copy constructor used?
A: When duplicating an object with the same state.

Q10: What happens if you define only a parameterized constructor?
A: Then the default constructor is not automatically created.

๐Ÿ“ MCQs

Q1. What constructor runs once per type?

  • Default constructor
  • Static constructor
  • Private constructor
  • Copy constructor

Q2. Which constructor has no parameters?

  • Copy constructor
  • Default constructor
  • Private constructor
  • None

Q3. Can a constructor be private?

  • No
  • Yes
  • Only in sealed classes
  • Only for interfaces

Q4. What is true about static constructors?

  • They can be overloaded
  • They run only once
  • They require parameters
  • They can be virtual

Q5. What constructor copies object state?

  • Default
  • Static
  • Copy constructor
  • None

Q6. What happens if no constructor is defined?

  • Error occurs
  • No object created
  • C# provides default
  • Requires static constructor

Q7. Which constructor is best for DI?

  • Static
  • Copy
  • Default
  • Parameterized

Q8. Which constructor is good for singleton?

  • Static
  • Private constructor
  • Default
  • Copy constructor

Q9. Can static constructors be inherited?

  • Yes
  • No
  • Only by abstract classes
  • Only in partial classes

Q10. Can static constructors take parameters?

  • Yes
  • No
  • Optional only
  • Depends on compiler

๐Ÿ’ก Bonus Insight

Using the right constructor type improves code clarity, prevents invalid object states, and simplifies object creation especially in design patterns like Factory and Singleton.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: