C# Interview Question #22

What are the different types of constructors in C#?

Classes, Constructors & Properties Junior Beginner

Quick Interview Answer

Common constructor forms include parameterless constructors, parameterized constructors, static constructors, and private constructors.

Detailed Explanation

Common constructor forms include parameterless constructors, parameterized constructors, static constructors, and private constructors.

A parameterless constructor takes no arguments. A parameterized constructor accepts values needed to initialize an object. A static constructor initializes static state and runs automatically before the type is first used. A private constructor prevents normal construction from outside the class and can be useful in factory or controlled-creation patterns.

C# does not have a special copy-constructor keyword, but a developer can create a constructor that accepts another instance of the same type.

Code Example

public Product() { }

public Product(string name)
{
    Name = name;
}

static Product()
{
    // Initialize static data
}

private Product(int internalId)
{
    Id = internalId;
}