What is a Constructor in C#?

๐Ÿ’ก Concept: Constructor

A constructor in C# is a special method that is automatically invoked when an object is created from a class. It is used to initialize the objectโ€™s state and allocate any resources needed.

๐Ÿ“˜ Quick Intro

Constructors share the name of the class and have no return type. They can be default (no parameters), parameterized (take input), static (run once per type), or copy (clone another object).

๐Ÿง  Analogy

Think of a constructor like the setup process when you buy a new phone: it's unboxed, charged, and pre-configured so it's ready to use โ€” just like how a constructor prepares an object for use right after it's created.

๐Ÿ”ง Technical Explanation

  • โœ… Constructors have the same name as the class and no return type.
  • โœ… Default constructors take no parameters.
  • โœ… Parameterized constructors allow setting fields during object creation.
  • โœ… Static constructors initialize static data and are called only once per type.
  • โœ… C# supports constructor chaining using this() and base().

๐ŸŽฏ Use Cases

  • ๐Ÿ”ง Initialize objects with required values.
  • ๐Ÿงฑ Provide default states for fields or properties.
  • ๐Ÿ“ฆ Dependency injection and service initialization.
  • โš™๏ธ Set up required data before the object can be used.

๐Ÿ’ป Code Example

public class Person {
    public string Name;
    public int Age;

    // Default constructor
    public Person() {
        Name = "Unknown";
        Age = 0;
    }

    // Parameterized constructor
    public Person(string name, int age) {
        Name = name;
        Age = age;
    }
}

var p1 = new Person();
var p2 = new Person("Alice", 30);

โ“ Interview Q&A

Q1: What is a constructor?
A: A special method that initializes an object of a class.

Q2: Can constructors have return types?
A: No, not even void.

Q3: How is a constructor different from a method?
A: It doesn't have a return type and is called automatically when an object is created.

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

Q5: Can we overload constructors?
A: Yes, constructor overloading is supported in C#.

Q6: What is constructor chaining?
A: Calling one constructor from another using this() or base().

Q7: When is a default constructor provided automatically?
A: When no constructors are explicitly defined in a class.

Q8: Can a constructor be private?
A: Yes, used in singleton or factory patterns.

Q9: What happens if a class has only parameterized constructors?
A: You must explicitly define a default constructor if needed.

Q10: Can a class have multiple constructors?
A: Yes, using constructor overloading.

๐Ÿ“ MCQs

Q1. What is a constructor in C#?

  • A static method
  • A property
  • A field
  • A special method for object initialization

Q2. What is the return type of a constructor?

  • void
  • int
  • string
  • None

Q3. Which keyword is used to chain constructors?

  • new
  • class
  • this
  • baseclass

Q4. Can constructors be overloaded?

  • No
  • Yes
  • Only static ones
  • Only default constructors

Q5. When is a constructor called?

  • On method call
  • At compile time
  • At the time of object creation
  • Never

Q6. What is a static constructor?

  • For method calls
  • Used in structs
  • Used to initialize static members
  • None

Q7. Which constructor type takes no parameters?

  • Static constructor
  • Virtual constructor
  • Default constructor
  • Chained constructor

Q8. What does constructor chaining use?

  • get/set
  • override
  • ref
  • this() or base()

Q9. What is true about private constructors?

  • Always public
  • Static only
  • They prevent external instantiation
  • Cause errors

Q10. Can a class have multiple constructors with different parameters?

  • No
  • Yes
  • Only one
  • If sealed

๐Ÿ’ก Bonus Insight

Understanding constructors is critical for clean object creation and maintenance. Combined with dependency injection, they ensure your objects are always in a valid state.

๐Ÿ“„ PDF Download

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

๐Ÿ” Navigation

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

Tags: