Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Initialization vs Instantiation in C#
In C#, initialization and instantiation are two fundamental concepts that are often confused. Initialization refers to assigning a value to a variable when it is declared, while instantiation refers to creating a new object instance using the new keyword.
Initialization
Initialization is the process of assigning a value to a variable at the time of declaration. This can be done for value types, reference types, and collections −
Value Type Initialization
using System;
class Program {
public static void Main() {
int val = 50;
double price = 99.99;
bool isActive = true;
char grade = 'A';
Console.WriteLine("Integer: " + val);
Console.WriteLine("Double: " + price);
Console.WriteLine("Boolean: " + isActive);
Console.WriteLine("Character: " + grade);
}
}
The output of the above code is −
Integer: 50 Double: 99.99 Boolean: True Character: A
Array Initialization
Arrays can be initialized in multiple ways using the new keyword or array literal syntax −
using System;
class Program {
public static void Main() {
// Array initialization with new keyword
int[] numbers1 = new int[] {1, 2, 3, 4, 5};
// Array initialization with literal syntax
int[] numbers2 = {10, 20, 30, 40, 50};
// String array initialization
string[] names = {"Alice", "Bob", "Charlie"};
Console.WriteLine("First array: " + string.Join(", ", numbers1));
Console.WriteLine("Second array: " + string.Join(", ", numbers2));
Console.WriteLine("Names: " + string.Join(", ", names));
}
}
The output of the above code is −
First array: 1, 2, 3, 4, 5 Second array: 10, 20, 30, 40, 50 Names: Alice, Bob, Charlie
Instantiation
Instantiation is the process of creating a new object instance of a class using the new operator. This allocates memory for the object and calls its constructor −
Example
using System;
class Student {
public string name;
public int age;
public Student() {
Console.WriteLine("Student object created");
}
public Student(string name, int age) {
this.name = name;
this.age = age;
Console.WriteLine("Student object created with parameters");
}
public void DisplayInfo() {
Console.WriteLine("Name: " + name + ", Age: " + age);
}
}
class Program {
public static void Main() {
// Instantiation using default constructor
Student s1 = new Student();
s1.name = "Alice";
s1.age = 20;
// Instantiation using parameterized constructor
Student s2 = new Student("Bob", 22);
s1.DisplayInfo();
s2.DisplayInfo();
}
}
The output of the above code is −
Student object created Student object created with parameters Name: Alice, Age: 20 Name: Bob, Age: 22
Comparison
| Initialization | Instantiation |
|---|---|
| Assigns a value to a variable | Creates a new object instance |
Can be done with or without new keyword |
Always requires the new keyword |
| Used for value types and reference types | Used only for reference types (objects) |
| Memory allocation depends on type | Always allocates memory on the heap |
Combined Example
Here's an example showing both initialization and instantiation together −
using System;
class Car {
public string brand;
public int year;
public Car(string brand, int year) {
this.brand = brand;
this.year = year;
}
public void Display() {
Console.WriteLine(brand + " " + year);
}
}
class Program {
public static void Main() {
// Variable initialization (value type)
int count = 3;
// Object instantiation with initialization
Car car1 = new Car("Toyota", 2020);
Car car2 = new Car("Honda", 2022);
Console.WriteLine("Total cars: " + count);
car1.Display();
car2.Display();
}
}
The output of the above code is −
Total cars: 3 Toyota 2020 Honda 2022
Conclusion
Initialization assigns values to variables at declaration time, while instantiation creates new object instances using the new keyword. Understanding the difference helps in proper memory management and object-oriented programming in C#.
