| C# Sharp QA-20 :👈 | 👉:XAML |
Constructors in C# and their Types |
What Are Constructors? Explain the Types of Constructors.
A constructor is a special member of a class that initializes an object when it is created. A constructor has the same name as the class and does not declare a return type.
1. Default Constructor
If no instance constructor is declared, C# generally provides an implicit parameterless constructor for a non-static class.
public class Employee
{
public string Name { get; set; }
}
// The implicit parameterless constructor can be used:
Employee employee = new Employee();
2. Parameterless Constructor
A parameterless constructor is explicitly declared without parameters.
public class Employee
{
public Employee()
{
Console.WriteLine("Employee created");
}
}
3. Parameterized Constructor
A parameterized constructor accepts arguments to initialize an object.
public class Employee
{
public string Name { get; }
public Employee(string name)
{
Name = name;
}
}
4. Copy Constructor
C# does not automatically provide a copy constructor, but you can define one that initializes a new object using another object of the same type.
public class Employee
{
public string Name { get; set; }
public Employee(string name)
{
Name = name;
}
public Employee(Employee other)
{
Name = other.Name;
}
}
This example copies the Name value. It is a shallow, member-by-member style copy for the data shown; objects with reference-type members may require a deliberate deep-copy strategy.
5. Static Constructor
A static constructor initializes static state for a type. It takes no parameters, has no access modifier, and is executed automatically by the runtime before the type is first used, subject to C# type-initialization rules.
public class Settings
{
public static string ApplicationName;
static Settings()
{
ApplicationName = "My Application";
}
}
6. Private Constructor
A private constructor prevents code outside the class from directly constructing instances through that constructor. It is useful for factory patterns and certain utility or singleton designs.
public class Utility
{
private Utility()
{
}
public static void Run()
{
Console.WriteLine("Running");
}
}
7. Primary Constructor
In C# 12 and later, classes and structs can declare a primary constructor in the type declaration.
public class Employee(string name)
{
public string Name { get; } = name;
}
Constructor Summary
| Constructor Type | Purpose |
|---|---|
| Default | Implicit parameterless constructor supplied when no instance constructor is declared. |
| Parameterless | Initializes an object without arguments. |
| Parameterized | Initializes an object using supplied arguments. |
| Copy | Creates an object based on another object. |
| Static | Initializes static type state. |
| Private | Restricts direct construction from outside the class. |
| Primary | Declares constructor parameters in the type declaration (C# 12+). |
Interview summary: Constructors initialize objects or type state. Common forms include parameterless, parameterized, copy, static, and private constructors; modern C# also supports primary constructors.
| C# Sharp QA-20 :👈 | 👉:XAML |