Last Updated : 11 Jan, 2025
Constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Constructors in C# are fundamental components of object-oriented programming. Like methods, It contains the collection of instructions that are executed at the time of Object creation. It is used to assign initial values to the data members of the same class. Some important points about Constructors are mentioned below:
Example:
class Geeks{Types of Constructor
// constructor of Geeks class
public Geeks( ) { }
}// Using of this constructor
Geeks obj = new Geeks();
There are different types of constructors in C#, each serving different purposes. These are the most commonly used:
A constructor with no parameters is called a Default constructor. A default constructor has every instance of the class to be initialized to the same values. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class.
Example:
C#
// Default constructor
using System;
class Geeks
{
// Initialize with default value
int num = 0;
// Initialize with an empty string
string name = "";
public Geeks()
{
Console.WriteLine("Constructor Called");
}
public static void Main()
{
// Invoke default constructor
Geeks geek1 = new Geeks();
// Display default values of fields
Console.WriteLine(geek1.name);
Console.WriteLine(geek1.num);
}
}
Constructor Called 02. Parameterized Constructor
A constructor having at least one parameter is called a parameterized constructor. It can initialize each instance of the class to different values.
Example:
C#
// Parameterized Constructor
using System;
class Geek {
// store name value
String n;
// store Id
int i;
// the values of passed arguments
// while object of that class created.
Geek(String n, int i)
{
this.n = n;
this.i = i;
}
public static void Main()
{
// It will invoke parameterized
Geek o = new Geek("GFG", 1);
Console.WriteLine("Name = " + o.n + " Id = " + o.i);
}
}
3. Copy Constructor
A copy constructor is used to create a new object by copying the values from an existing object of the same class. Useful when you need to duplicate an object or create a new object based on the state of another.
Example:
C#
// Copy constructor
using System;
class Geeks
{
private string month;
private int year;
// declaring Copy constructor
public Geeks(Geeks s)
{
month = s.month;
year = s.year;
}
// Instance constructor
public Geeks(string month, int year)
{
this.month = month;
this.year = year;
}
// Get details of Geeks
public string Details
{
get
{
return "Month: " + month.ToString()
+ "\nYear: " + year.ToString();
}
}
public static void Main()
{
// Create a new Geeks object.
Geeks g1 = new Geeks("June", 2018);
// here is g1 details is copied to g2.
Geeks g2 = new Geeks(g1);
Console.WriteLine(g2.Details);
}
}
Month: June Year: 20184. Private Constructor
If a constructor is created with a private specifier is known as Private Constructor. It is not possible for other classes to derive from this class and also it’s not possible to create an instance of this class. Some important points regarding the topic is mentioned below:
Note: Access modifiers can be used in constructor declaration to control its access i.e. which other class can call the constructor. Private Constructor is one of it's example.
Example
C#
// Private constructor
using System;
public class Geeks
{
// private Constructor
private Geeks()
{
Console.WriteLine("from private constructor");
}
public static int count_geeks;
public static int geeks_Count()
{
return ++count_geeks;
}
public static void Main()
{
// If you uncomment the following
// statement, it will generate
// an error because the constructor
// is unaccessible:
// Geeks s = new Geeks(); // Error
Geeks.count_geeks = 99;
// Accessing without any
// instance of the class
Geeks.geeks_Count();
Console.WriteLine(Geeks.count_geeks);
// Accessing without any
// instance of the class
Geeks.geeks_Count();
Console.WriteLine(Geeks.count_geeks);
}
}
5. Static Constructor
Static Constructor has to be invoked only once in the class and it has been invoked during the creation of the first reference to a static member in the class. A static constructor is initialized static fields or data of the class and is to be executed only once.
Points To Remember:
Example:
C#
// Static constructor
using System;
class Geeks
{
// It is invoked before the first
// instance constructor is run.
static Geeks()
{
Console.WriteLine("Static Constructor");
}
public Geeks(int i)
{
Console.WriteLine("Instance Constructor " + i);
}
public string geeks_detail(string name, int id)
{
return "Name: " + name + " id: " + id;
}
public static void Main()
{
// Here Both Static and instance
// constructors are invoked for
// first instance
Geeks obj = new Geeks(1);
Console.WriteLine(obj.geeks_detail("GFG", 1));
Geeks obj1 = new Geeks(2);
Console.WriteLine(
obj1.geeks_detail("GeeksforGeeks", 2));
}
}
Static Constructor Instance Constructor 1 Name: GFG id: 1 Instance Constructor 2 Name: GeeksforGeeks id: 2
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4