ุงูู Constructor ูู ุนุจุงุฑุฉ ุนู method ุฎุงุตุฉ ู
ูุฌูุฏุฉ ุฌูู ุงูู classุ ูู
ุณุคููุฉ ุนู ุชุญุท ููู
ุฌูุง ุงูู Variables ุงูุฎุงุตุฉ ุจุงูููุงุณ ุฏุง ูุจูููู ุนูููุง (initializing the variables).
ุจูุชู
ุงุณุชุฏุนุงุก ูุจูุญุตู Call ุงูู constructor ุชููุงุฆููุง ุฃูู ู
ุง ุจูุนู
ู instance ุฃู object ุฌุฏูุฏ ู
ู ุงูููุงุณ.
ุจูุชููู ู ู ุงููุ
Class Test
{
// AccessModeifier ClassName(Parameter)
public Test()
{
}
}- ุงูุงุณู
: ุงุณู
ุงูู
constructorูุงุฒู ูููู ูู ูู ุจุงูุธุจุท ุงุณู ุงููclassุงููู ูู ู ูุฌูุฏ ููู. ู ุชูุฏุฑุด ุชุบูุฑู. - ุงูู
Return Type: ุงููconstructorู ุด ุจูุฑุฌุน ุฃู ููู ุฉุ ูุนุดุงู ูุฏู ู ูููุนุด ูููู ูู ุฃูreturn typeุ ุญุชูvoidุจุฑุถู ูุฃ. - ุงูู Access Modifiers: ูู ุงูุนุงุฏุฉุ ุจูููู
publicุนุดุงู ููุฏุฑ ูุนู ูobjectsู ู ุงูููุงุณ ูู ุฃู ู ูุงู. ููู ู ู ูู ููููprivateุฃูprotectedูู ุญุงูุงุช ุฎุงุตุฉ ุฒู ุจุนุถ ุงูู Design Pattern. - ุงูุงุณุชุฎุฏุงู
: ุจูุฏู ููู
ุงุจุชุฏุงุฆูุฉ ููู
fieldsุฃู ุงููpropertiesุจุชุงุน ุงูููุงุณ.
How Does it Work Without a Defined Constructor?
ู
ู
ูู ุชุณุฃู ููุณู: ุฅุญูุง ุนู
ููุง classes ูุจู ูุฏู ูู
ูุชุจูุงุด ูููุง constructorุ ูู
ุน ุฐูู ููุง ุจููุฏุฑ ูุนู
ู object ุนุงุฏู. ุฅุฒุงู ุฏู ุญุตูุ
ุงูุณุจุจ ูู ุฅูู ูู ุงูู
ุจุฑู
ุฌ ู
ุนุฑูุด ุฃู constructor ุจููุณูุ ุงูู compiler ุจูุถูู ูุงุญุฏ ุชููุงุฆู ููุช ุงูู compilation. ุงูู constructor ุฏู ุจูุณู
ูู Implicit Constructor (ุจููููุงุก ุถู
ูู).
graph LR subgraph Before Compilation A["class Test { int i;}"] end subgraph After Compilation by Compiler B["public Test() i = 0; // Default initialization"] end A -->|"Compiler Adds"| B
ุงูู Implicit Constructor ุฏู ุจูููู public ูู
ู ุบูุฑ parameters (parameter-less)ุ ููุธููุชู ุฅูู ูุนู
ู ุชููุฆุฉ ููู ู
ุชุบูุฑุงุช ุงูููุงุณ ุจููู
ูุง ุงูุงูุชุฑุงุถูุฉ (default values).
- ุงูุฃููุงุน ุงูุฑูู
ูุฉ (
int,long,float,double):0 bool:falsestring,object:nullchar:'\0'
Why Do We Need to Define a Constructor Explicitly?
ุทุงูู
ุง ุงูู compiler ุจูุนู
ู ูุงุญุฏ ุจุงูููุงุจุฉ ุนููุงุ ุฅูู ูุฒู
ุฉ ุฅููุง ููุชุจ constructor ุจููุณูุง (explicitly)ุ
ุงูู
ุดููุฉ ูู ุงูู Implicit Constructor (ุฃู ุญุชู ุงูู Default Constructor ุงููู ุจูุนู
ูู ุจููู
ุซุงุจุชุฉ) ูู ุฅูู ุจูุนู
ู ุชููุฆุฉ ููู ุงูู objects ุจููุณ ุงูููู
.
ู ุซุงู:
class First
{
public int x = 100;
}
class Test
{
static void Main(string[] args)
{
First f1 = new First();
First f2 = new First();
First f3 = new First();
// All objects will have x = 100
Console.WriteLine($"{f1.x}, {f2.x}, {f3.x}"); // Output: 100, 100, 100
}
}ููุงุ ูู object ุจูุนู
ูู ุจูุงุฎุฏ ููุณ ุงูููู
ุฉ 100. ูู ุนุงูุฒูู ูุฏู ููู object ููู
ุฉ ู
ุฎุชููุฉ ููุช ุฅูุดุงุฆูุ ููุง ุจูุฌู ุฏูุฑ ุงูู Explicit Parameterized Constructor. ุฏู ุจูุณู
ุญููุง ูู
ุฑุฑ ููู
ู
ุฎุชููุฉ ููู object ุจูุนู
ูู.
class Second
{
public int x;
// Explicit Parameterized Constructor
public Second(int value)
{
this.x = value;
}
}
class Test
{
static void Main(string[] args)
{
// Now we can initialize each instance with a different value
Second s1 = new Second(100);
Second s2 = new Second(200);
Second s3 = new Second(300);
Console.WriteLine($"{s1.x}, {s2.x}, {s3.x}"); // Output: 100, 200, 300
}
}Types of Constructors
ููู ุฎู
ุณ ุฃููุงุน ุฑุฆูุณูุฉ ู
ู ุงูู constructors ูู ูุบุฉ C#:
- Default (Parameter-less) Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- Private Constructor
1. Default (Parameter-less) Constructor
ุฏู ุงูู constructor ุงููู ู
ุด ุจูุงุฎุฏ ุฃู parameters. ููู ููุนูู:
- ุงูู System-Defined Default Constructor: ุฏู ุงููู ุงูู
compilerุจูุนู ูู ุชููุงุฆููุง ูู ุฅูุช ู ูุชุจุชุด ุฃูconstructorุฎุงูุต. ุจููููpublicูุจูุนู ู ุชููุฆุฉ ููู ุชุบูุฑุงุช ุจููู ูุง ุงูุงูุชุฑุงุถูุฉ. - ุงูู User-Defined Default Constructor: ุฏู ุงููู ุฅูุช ุจุชูุชุจู ุจููุณู ุจุณ ู
ู ุบูุฑ
parameters. ุจูุณุชุฎุฏู ู ุนุดุงู ูุฏู ููู ุงุจุชุฏุงุฆูุฉ ุซุงุจุชุฉ ููู ุงููobjectsุฃู ูููุฐlogicู ุนูู ู ุน ูู ุนู ููุฉ ุฅูุดุงุก.
ู ุซุงู User-Defined:
class Employee
{
public string Name;
// User-Defined Default Constructor
public Employee()
{
Name = "Default Employee";
}
}ุงูุนูุจ ูู ุงูููุน ุฏู ูู ุฅู ูู ุงูู objects ุงููู ูุชุนู
ููุง ูุชุงุฎุฏ ููุณ ุงูููู
ุงูุซุงุจุชุฉ.
2. Parameterized Constructor
ุฏู ุงูู constructor ุงููู ุจูุงุฎุฏ parameters. ุจูุณุชุฎุฏู
ู ุนุดุงู ูู
ุฑุฑ ููู
ู
ุฎุชููุฉ ููู object ุจูุนู
ููุ ูุฏู ุจูุฏููุง ู
ุฑููุฉ ุฃูุจุฑ.
class Employee
{
public int Id;
public string Name;
// User Defined Parameterized Constructor
public Employee(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
// Usage
Employee emp1 = new Employee(101, "Ahmed");
Employee emp2 = new Employee(102, "Mona");3. Copy Constructor
ุฏู ููุน ุฎุงุต ู
ู ุงูู Parameterized Constructor ุจูุงุฎุฏ parameter ูุงุญุฏ ุจุณุ ุจูููู object ู
ู ููุณ ููุน ุงูููุงุณ. ูุธููุชู ุฅูู ููุณุฎ ุจูุงูุงุช object ู
ูุฌูุฏ ุจุงููุนู ูู object ุฌุฏูุฏ. ุฏู ู
ููุฏ ูู ุนุงูุฒ ุชุนู
ู ูุณุฎุฉ ู
ู object ู
ุนูุฏ ู
ู ุบูุฑ ู
ุง ุชุนูุฏ ุชู
ุฑูุฑ ูู ููู
ู ู
ู ุชุงูู.
class Employee
{
public int Id;
public string Name;
// Parameterized Constructor
public Employee(int id, string name)
{
this.Id = id;
this.Name = name;
}
// Copy Constructor
public Employee(Employee empToCopy)
{
this.Id = empToCopy.Id;
this.Name = empToCopy.Name;
}
}
// Usage
Employee originalEmp = new Employee(101, "Ahmed");
// Create a new employee by copying data from the original
Employee copiedEmp = new Employee(originalEmp); 4. Static Constructor
ุฏู constructor ุจูุนุฑูู ุจุงุณุชุฎุฏุงู
static modifierุ ููู ููุงุนุฏ ูุณููู ุฎุงุต ุฌุฏูุง.
- ู
ุณุคูููุชู: ุจูุญุท ููู
ูู ุงูู
static fieldsุจุชุงุนุฉ ุงูููุงุณุ ุฃู ุชูููุฐ ุฃูlogicู ุญุชุงุฌ ูุดุชุบู ู ุฑุฉ ูุงุญุฏุฉ ุจุณ ุนูู ู ุณุชูู ุงูููุงุณ ููู. - ุงูุงุณุชุฏุนุงุก: ุจูุชูุงุฏู ุจุดูู ุถู
ูู (
implicitly) ู ุฑุฉ ูุงุญุฏุฉ ุจุณ ูู ุฏูุฑุฉ ุญูุงุฉ ุงูุชุทุจูู (ูุนูู ูู ุง ูุดุบู ุงูุงุจูููุดู ููููู ูุงูุงุฎุฑ ูู ุฏู ุฏูุฑุฉ ุงูุญูุงุฉ). - ุชูููุช ุงูุชูููุฐ: ุจูุชููุฐ ุชููุงุฆููุง ูุจู ู
ุง ุชุนู
ู ุฃูู
objectู ู ุงูููุงุณุ ุฃู ูุจู ู ุง ุชูุตู ูุฃูmemberstatic(ู ุชุบูุฑ ุฃูmethod) ูู ุงูููุงุณ ุฏู ูุฃูู ู ุฑุฉ. ูู ุฃูู ุญุงุฌุฉ ุจุชุชููุฐ ูู ุงูููุงุณ. - ููุงุนุฏู:
- ู
ูููุนุด ูุงุฎุฏ ุฃู
access modifiers(ูุงpublicููุงprivate). - ูุงุฒู
ูููู ู
ู ุบูุฑ
parameters. - ู ูููุนุด ูุณุชุฏุนูู ุจููุณูุง ุจุดูู ุตุฑูุญ.
- ู
ูููุนุด ูุนู
ูู
overloadingุ ูู ู ูู ูููู ููู ูุงุญุฏ ุจุณ ูู ุงูููุงุณ. - ุจููุฏุฑ ููุตู ููู
static membersุจุณ.
- ู
ูููุนุด ูุงุฎุฏ ุฃู
public class DatabaseConnection
{
// Static Constructor
static DatabaseConnection()
{
Console.WriteLine("Static Constructor Executed: Initializing database connection settings...");
}
// Instance Constructor
public DatabaseConnection()
{
Console.WriteLine("Instance Constructor Executed.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main method started.");
// Static constructor runs here, before the first instance is created
DatabaseConnection conn1 = new DatabaseConnection();
DatabaseConnection conn2 = new DatabaseConnection();
Console.WriteLine("Main method finished.");
}
}Output:
Main method started.
Static Constructor Executed: Initializing database connection settings...
Instance Constructor Executed.
Instance Constructor Executed.
Main method finished.
5. Private Constructor
ุฏู constructor ุจูุนุฑูู ุจุงุณุชุฎุฏุงู
private access modifier.
- ุชุฃุซูุฑู ุงูุฃุณุงุณู: ุจูู
ูุน ุงููุง ูุนู
ู
objectู ู ุงูููุงุณ ุฏู ู ู ุจุฑู ุงูููุงุณ. ููู ููุฏุฑ ูุนู ูobjectsุฌูู ููุณ ุงูููุงุณ ุนุงุฏู. - ุงููุฑุงุซุฉ: ู
ุด ุจูู
ูุน ุงููุฑุงุซุฉ ุจุดูู ู
ุทูู. ูู ููู
public constructorุชุงููุ ู ู ูู ุชูุฑุซ ุงูููุงุณ. ููู ูู ูู ุงููconstructorsุงููู ูููprivateุ ุณุงุนุชูุง ู ุด ูุชูุฏุฑ ุชูุฑุซู ู ู ููุงุณ ุฎุงุฑุฌู. ูู ุนุงูุฒ ุชู ูุน ุงููุฑุงุซุฉ ุชู ุงู ูุงุ ุงุณุชุฎุฏูsealed class. - ุงูุงุณุชุฎุฏุงู
ุงูุฃุดูุฑ: ุชุทุจูู ุงูู Singleton Design Patternุ ููู ูู
ุท ุจูุถู
ู ุฅูู ุชุนู
ู
objectูุงุญุฏ ุจุณ ู ู ุงูููุงุณ ุฏู ุนูู ู ุณุชูู ุงูุชุทุจูู ููู.
public sealed class Singleton
{
private static Singleton _instance = null;
private static int counter = 0;
// The private constructor prevents creating instances from outside
private Singleton()
{
counter++;
Console.WriteLine($"Singleton instance created. Counter: {counter}");
}
public static Singleton GetInstance()
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}Advanced Concepts
Constructor Overloading
ุงูู Overloading ู
ุนูุงู ุฅูู ูููู ุนูุฏู ุฃูุชุฑ ู
ู constructor ูู ููุณ ุงูููุงุณุ ูู ูุงุญุฏ ูู signature ู
ุฎุชููุฉ (ูุนูู ุนุฏุฏ ุฃู ููุน ุฃู ุชุฑุชูุจ ุงูู parameters ู
ุฎุชูู).
ูุฏุง ููุน ู ู ุงููุงุน ุงูู Method Overloading ุงููู ูู ุนุจุงุฑุฉ ุนู Compile-time Polymorphism
public class Car
{
// Overloaded constructors
public Car () { Console.WriteLine("Default car created."); }
public Car (string model) { Console.WriteLine($"{model} created."); }
public Car (string model, int year) { Console.WriteLine($"{model} from {year} created."); }
}Constructor Chaining
ุฃุญูุงููุง ุจูููู ุนูุฏู ููุฏ ู
ูุฑุฑ ูู ุฃูุชุฑ ู
ู constructor. ุนุดุงู ุชุชุฌูุจ ุงูุชูุฑุงุฑ ุฏู ูุชุฎูู ููุฏู ุฃูุถูุ ู
ู
ูู ุชุณุชุฎุฏู
Constructor Chainingุ ูุฏู ู
ุนูุงู ุฅู constructor ููุงุฏู ุนูู constructor ุชุงูู ูู ููุณ ุงูููุงุณ. ุจูุนู
ู ุฏู ุจุงุณุชุฎุฏุงู
ููู
ุฉ this.
public class Customer
{
public List<Order> Orders;
public int ID;
public string Name;
// 1. Default constructor initializes the list
public Customer()
{
Orders = new List<Order>();
}
// 2. This constructor calls the default one first using ': this()'
public Customer(int id)
: this()
{
this.ID = id;
}
// 3. This constructor calls the one with the 'id' parameter first
public Customer(int id, string name)
: this(id)
{
// By the time this code runs, the list is initialized and the ID is set.
this.Name = name;
}
}ู
ูุญูุธุฉ: ุงูู constructor ุงููู ุจูุนู
ูู chain ุจูุชููุฐ ุงูุฃููุ ูุจุนุฏูู ุงูููุฏ ุงููู ุฌูู ุงูู constructor ุงูุญุงูู ุจูุชููุฐ.
Important Distinctions
Struct vs. Class Constructors
- ุงูู
Class: ุงููdefault constructor(ุงูุถู ูู) ุจูุชุนู ู ุจุณ ูู ุฃูุช ู ุนู ูุชุด ุฃูconstructorุจููุณู. ูู ุนู ูุช ูุงุญุฏุ ุงูุงูุชุฑุงุถู ุจูุชูุบู. - ุงูู
Struct: ุงููdefault constructorุจูุชุนู ู ุฏุงูู ูุง ูู ุชูุฏุฑุด ุชู ูุนู ุฃู ุชุบูุฑู. ูู ุนู ูุชconstructorุจูparametersุ ูุงุฒู ุชุนู ูinitializeููู ุงููfieldsุงููู ุฌูู ุงููstruct.
Static vs. Non-Static Constructors
| ุงูู ูุฒุฉ | Static Constructor | Non-Static (Instance) Constructor |
|---|---|---|
| ุงูุชุนุฑูู | ุจูุณุชุฎุฏู
static modifier | ู
ุด ุจูุณุชุฎุฏู
static modifier |
| ุงูู ุณุคูููุฉ | ุจูุญุท ููู
ูู ุงูู static data | ุจูุญุท ููู
ูู ุงูู non-static (instance) data |
| ุงูุงุณุชุฏุนุงุก | ุถู
ูู (Implicit) | ุตุฑูุญ (Explicit) |
| ุนุฏุฏ ู ุฑุงุช ุงูุชูููุฐ | ู ุฑุฉ ูุงุญุฏุฉ ุจุณ | ุตูุฑ ุฃู ุฃูุชุฑ (ู
ุน ูู instance ุฌุฏูุฏ) |
| ุงูู Parameters | ู
ูููุนุด ูุงุฎุฏ parameters | ู
ู
ูู ูุงุฎุฏ parameters |
| ุงูู Overloading | ู
ูููุนุด ูุนู
ูู overloading | ู
ู
ูู ูุนู
ูู overloading |
ุงูู Access Modifier | ู
ูููุนุด ูุงุฎุฏ ุฃู access modifier | ู
ู
ูู ูุงุฎุฏ ุฃู access modifier (public, private, etc.) |