LearnThatStack Ace your next interview
C# · question
Question 8 of 112

What are constructors and their types in C#?

beginner
← All C# questions
Re-explain

A constructor is a special method that runs when an object is created and sets up its state. It has the class name and no return type.

  • A default constructor takes no parameters. The compiler adds one only when the class declares no constructor.
  • A parameterized constructor takes values to initialize the object.
  • A static constructor initializes static members. It runs once per type, before first use.
  • A private constructor stops outside code from creating instances, as in a singleton.
  • C# has no built-in copy constructor, so you write one yourself.
public class Person
{
    // Parameterized constructor
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
    // Static constructor
    static Person()
    {
        // Initialize static members
    }
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How do this(...) and base(...) work, and in what order do the constructors run? this(...) reuses another constructor in the same class, base(...) picks which base constructor runs, and the base body runs before the derived body.
  • When exactly does a static constructor run, and what happens if it throws? It runs once per type, before the first instance or static member access, and you never call it. If it throws, you get TypeInitializationException.
  • How would you use a private constructor to build a thread-safe singleton? Use a private constructor and one static instance, made safe with a static readonly field or Lazy<T>. An unlocked null check is not thread-safe.
  • Why is it risky to call a virtual method from a constructor? The override runs before the derived constructor body, so it can read fields that body has not set yet. Keep constructors to plain setup.

What you can say

  1. A constructor is a special method that runs when you create an object, and its job is to set up that object's state.
  2. It has the same name as the class and no return type, not even void.
  3. A default constructor takes no parameters, and a parameterized one takes the values the object needs, like a Person that needs a name and an age.
  4. A static constructor sets up static members, and it runs once per type, before the type is first used.
  5. A private constructor stops outside code from creating instances, which is how you build a singleton.
  6. One catch is that the compiler adds a default constructor only when you declare none, and C# has no built-in copy constructor, so you write that one yourself.

Weak answers to avoid

  • Claims there is always a default constructor The compiler adds one only when the class declares no constructor. Add a parameterized one and new Person() stops compiling unless you write a parameterless one too.
  • Claims C# has a built-in copy constructor It has none. Assigning a class variable copies the reference, not the object, so you write a copy constructor yourself when you need an independent copy.
  • Vague about when a static constructor runs Say it runs once per type, before first use, and code never calls it. Saying it runs for every object shows you have not used one.
  • Lists the types with no reason to use them Give each one a job, such as a private constructor for a singleton or a parameterized one so an object never exists half set up.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime