Difference Between Initialization And Instantiation In C#

In this article, we will discuss the difference between Initialization and Instantiation in C#. But before discussing the differences, we must know about the Initialization and Instantiation in C# with their syntax and examples.

What is the Initialization?

In C#, initialization is a process in which the developer assigns initial values to variables. Whenever a programmer wants to use some variables, they must be initialized with some initial values.

Syntax for the initialization:

It has the following syntax:

Example

int variable1 = 100;
String variable2 = "Hello, World";

Some common methods for initialization:

Several common method for initializations are as follows:

  • Constructor Initialization
  • Example
    
    DateTime currentDate = new DateTime(2024, 3, 19);
    
  • Object Initialization
  • Example
    
    Person person = new Person { Name = "Ramu", Age = 30 };
    
  • Collection Initialization
  • Example
    
    Dictionary<string, int> dictionary = new Dictionary<string, int> { { "One", 1 }, { "Two", 2 } };
    
  • Array Initialization
  • Example
    
    int[] array = new int[] { 1, 2, 3, 4, 5 };
    

    What is the Instantiation?

In C#, instantiation is a process of creating an object or instance of a class in the program. Here, we allocate the memory to objects and we initialize the members of the created object. The newly created object can access to all members and member functions in the class, such as methods, operators, and fields etc.

Syntax for the instantiation:

It has the following syntax:

Example

objectName = new className(parameters);

Some common methods of Instantiation:

Several common method for instantiation are as follows:

  • Using new keyword
  • Example
    
    MyClass obj = new MyClass();
    
  • Using clone or copy constructors
  • Example
    
    MyClass original = new MyClass(10);
    MyClass clone = (MyClass)original.Clone();
    
  • Using Reflection
  • Example
    
    Type type = typeof(MyClass);
    MyClass obj = (MyClass)Activator.CreateInstance(type);
    

    Example:

Let us take a C# program to illustrate both Initialization and Instantiation .

Example

using System;
class Car {
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    // Constructor for initialization
    public Car(string make, string model, int year) {
        Make = make;
        Model = model;
        Year = year;
    }
    public void DisplayDetails() {
        Console.WriteLine("Car Details:");
        Console.WriteLine("Make: " + Make);
        Console.WriteLine("Model: " + Model);
        Console.WriteLine("Year: " + Year);
    }
}
class Program {
    static void Main() {
        // Instantiating Car object and initializing its properties
        Car myCar = new Car("Toyota", "Corolla", 2020);
        // Display car details
        myCar.DisplayDetails();
    }
}

Output:

Explanation:

In the above program, there are two classes: Main and Car . In the Car class, three variables are two strings and one integer. They are initialized in the constructor. This class also contains one display method for displaying the variables in that class. In the Main class, an object is created and initialized with some values to the variables. Here, the creation of the object is used instantiation. After that, with the help of the object, the DisplayDetails method is accessed to print all the members in the class.

Difference between Initialization and Instantiation in C#

There are several difference between the Initialization and Instantiation in C#. Some main differences between the Initialization and Instantiation in C# are as follows:

Features Initialization Instantiation
Definition It gives the initial values to the variables when declared. It creates the object to the class.
Syntax Datatype variableName = value; className objectName = new className();
Time of action It happens at the point of declaration. It happens at runtime.
Application It is applied to both the variables and objects. It is mostly applied to create objects.
Memory allocation It does not necessarily involve memory allocation.int x =5;no memory allocation is required It involves in memory allocation.Myclass obj= new Myclass();Memory is allocated to the object
Types It is assigned to integers, strings, and floating point datatypes etc. These are specific to the classes, which are created by the programmer.
Default values It contains some default values. These is not defaulting values to the objects.
Mutable vs Immutable It is used for both mutable and immutable variables.Mutable: int x= 5Immutable: string s= "Java" It creates mutable objects or instancesMutableMyClass obj = new MyClass();
Parameterized action It involves parameterized assignment to variables. It involves parameterized assignment to objects.
Compile time Vs Runtime It can happen at compile time or run time. It happens at runtime only.
Inheritance Inheritance is not applicable. It involves the inheritance of the properties form the parent classes.
Accessibility These are accessed in various scopes.Example:public static int globalVar = 10; These are accessed within methods and constructorsExample:static void Main(){// Accessing class-scoped methodMyClass.ClassMethod();}

Input Required

This code uses input(). Please provide values below: