Constructors in C# - GeeksforGeeks

Constructors in C#

Last Updated : 16 Oct, 2026

A constructor is a special method of a class that is called automatically when an object is created. It is used to initialize the data members of an object.

  • A constructor has the same name as its class and does not have a return type, not even void.
  • Constructors can have parameters and can be overloaded to initialize objects in different ways.

Example: Program to demonstrates how to create and use a constructor.

C#
using System;

class Student
{
    string name;

    // Constructor
    public Student(string name)
    {
        this.name = name;
    }

    void Display()
    {
        Console.WriteLine("Name: " + name);
    }

    static void Main()
    {
        Student s1 = new Student("Amay");
        s1.Display();
    }
}

Output
Name: Amay

Explanation: The Student constructor accepts a string parameter and initializes the name field. When new Student("Amay") is executed, the constructor is called automatically.

Syntax

class ClassName{
public ClassName(){
// Initialization code
}
}

Types of Constructor

There are several types of constructors:

dns_caching
Types of Constructors

1. Default Constructor

A default constructor is a constructor that takes no parameters. In C#, if no instance constructor is declared, the compiler provides a public parameterless constructor for a class.

  • It initializes an object when it is created without arguments.
  • If no constructor is explicitly defined, the compiler provides a parameterless constructor that initializes fields to their default values.

Example: Program to demonstrates a default constructor provided by the compiler.

C#
using System;

class Student
{
    public string name;
    public int age;
}

class GFG
{
    static void Main()
    {
        Student s = new Student();

        Console.WriteLine(s.name == null ? "null" : s.name);
        Console.WriteLine(s.age);
    }
}

Output:

Solution.cs(6,16): warning CS0649: Field 'Student.age' is never assigned to, and will always have its default value 0
Solution.cs(5,19): warning CS0649: Field 'Student.name' is never assigned to, and will always have its default value null

Explanation: No constructor is declared in the Student class, so the compiler provides a parameterless constructor. The fields receive their default values: null for string and 0 for int.

Syntax

class ClassName{
public ClassName(){
// Initialization code
}
}

Note: If you declare an instance constructor yourself, the compiler does not automatically provide the default parameterless constructor.

2. Parameterized Constructor

A parameterized constructor is a constructor that accepts one or more parameters. It allows objects to be initialized with user-defined values during creation.

  • It initializes fields using values passed as arguments.
  • It helps create objects with different initial values.

Example: Program demonstrates a parameterized constructor.

C#
using System;

class Geeks
{
    string name;
    int id;

    // Parameterized Constructor
    public Geeks(string name, int id)
    {
        this.name = name;
        this.id = id;
    }

    // Method to display object data
    public void Display()
    {
        Console.WriteLine("GeekName: " + name +
                          " and GeekId: " + id);
    }
}

class GFG
{
    static void Main()
    {
        Geeks geek1 = new Geeks("Sweta", 68);
        geek1.Display();
    }
}

Output
GeekName: Sweta and GeekId: 68

Explanation: The Geeks constructor accepts name and id as parameters. The this keyword refers to the current object and assigns the supplied values to its fields.

Syntax

class ClassName{
public ClassName(dataType parameter){
// Initialize fields
}
}

3. Copy Constructor

A copy constructor is a user-defined constructor that creates a new object by copying data from an existing object of the same class. C# does not automatically provide a special copy constructor, so it must be defined explicitly.

  • It accepts an object of the same class as a parameter.
  • It copies the required field values from the existing object to the new object.

Example: Program to demonstrates how to create an object using a copy constructor.

C#
using System;

class Geeks
{
    public string name;
    public int id;

    // Parameterized Constructor
    public Geeks(string name, int id)
    {
        this.name = name;
        this.id = id;
    }

    // Copy Constructor
    public Geeks(Geeks obj)
    {
        this.name = obj.name;
        this.id = obj.id;
    }
}

class GFG
{
    static void Main()
    {
        Console.WriteLine("First Object");

        Geeks geek1 = new Geeks("Sweta", 68);
        Console.WriteLine("GeekName: " + geek1.name +
                          " and GeekId: " + geek1.id);

        Console.WriteLine();

        Geeks geek2 = new Geeks(geek1);

        Console.WriteLine("Copy Constructor used Second Object");
        Console.WriteLine("GeekName: " + geek2.name +
                          " and GeekId: " + geek2.id);
    }
}

Output
First Object
GeekName: Sweta and GeekId: 68

Copy Constructor used Second Object
GeekName: Sweta and GeekId: 68

Explanation: The copy constructor creates geek2 by copying the name and id values from geek1. Both objects contain the same field values but are separate objects.

Syntax

class ClassName{
public ClassName(ClassName obj){
// Copy field values from obj
}
}

Note: This example performs a shallow copy of the selected fields. If fields refer to mutable objects, those references are copied rather than the referenced objects themselves.

4. Static Constructor

A static constructor is used to initialize static data members of a class. It is called automatically by the runtime before the class is first used in a way that requires static initialization.

  • It does not have an access modifier or parameters.
  • It is executed automatically and typically runs only once for a type during an application domain's lifetime.

Example: Program demonstrates a static constructor.

C#
using System;

class GFG
{
    static GFG()
    {
        Console.WriteLine("Static constructor called");
    }

    public static void DisplayMessage()
    {
        Console.WriteLine("Hello from GFG class!");
    }
}

class Program
{
    static void Main()
    {
        GFG.DisplayMessage();
    }
}

Output
Static constructor called
Hello from GFG class!

Explanation: The static constructor runs automatically before DisplayMessage() is first called. It initializes any required static data for the class.

Syntax

class ClassName{
static ClassName(){
// Initialize static members
}
}

5. Private Constructor

A private constructor is a constructor declared with the private access modifier. It prevents code outside the class from directly creating instances using that constructor.

  • It is commonly used in utility classes that contain only static members.
  • It can be used in the Singleton pattern to control object creation.

Example: Program demonstrates a private constructor.

C#
using System;

class GFG
{
    // Private constructor
    private GFG()
    {
        Console.WriteLine("Private constructor called");
    }

    public static void DisplayMessage()
    {
        Console.WriteLine("Hello from GFG class!");
    }
}

class Program
{
    static void Main()
    {
        // GFG obj = new GFG(); // Error: constructor is private
        GFG.DisplayMessage();
    }
}

Output
Hello from GFG class!

Explanation: The GFG constructor is private, so Main() cannot directly create an instance of the class. However, its public static method can be called using the class name.

Syntax

class ClassName{
private ClassName(){
// Initialization code
}
}

Constructor Overloading

Constructor overloading allows a class to define multiple constructors with different parameter lists. It provides different ways to initialize objects based on the arguments supplied during object creation.

  • The constructors must differ in their parameter lists, such as the number, types, or order of parameters.
  • The appropriate constructor is selected at compile time based on the arguments passed when creating an object.

Example: Program to demonstrates constructor overloading using different parameter lists.

C#
using System;

class Geeks
{
    // Constructor with one string argument
    public Geeks(string name)
    {
        Console.WriteLine("Constructor with one argument - String: " + name);
    }

    // Constructor with two arguments
    public Geeks(string name, int age)
    {
        Console.WriteLine("Constructor with two arguments: String and Integer: " +
                          name + " " + age);
    }

    // Constructor with one long argument
    public Geeks(long id)
    {
        Console.WriteLine("Constructor with one argument - Long: " + id);
    }
}

class GFG
{
    static void Main()
    {
        Geeks geek1 = new Geeks("Sweta");
        Geeks geek2 = new Geeks("Amiya", 28);
        Geeks geek3 = new Geeks(325614567L);
    }
}

Output
Constructor with one argument - String: Sweta
Constructor with two arguments: String and Integer: Amiya 28
Constructor with one argument - Long: 325614567

Explanation: The Geeks class defines three constructors with different parameter lists. The compiler selects the appropriate constructor based on the arguments used to create each object.

Syntax

class ClassName{
public ClassName(string name){
// Initialization code
}

public ClassName(string name, int age){
// Initialization code
}
}

Advantages of Constructors

  • Automatic Initialization: Constructors initialize objects automatically when they are created.
  • Code Reusability: Avoids repeating initialization code for every object.
  • Flexible Initialization: Constructor overloading allows objects to be initialized with different values.
  • Data Validation: Constructors can validate input values before assigning them to object fields or properties.
  • Improved Readability: Keeps object initialization logic organized and easy to understand.

Limitations of Constructors

  • Cannot Be Inherited: Constructors are not inherited by derived classes, although base constructors can be called.
  • No Return Type: Constructors cannot specify a return type, including void.
  • Limited Flexibility: Values initialized through a constructor cannot be changed later unless the class provides a way to modify them.
  • Complex Initialization: Constructors with too many parameters or extensive logic can make code difficult to maintain.
Comment

Explore