Difference Between Initialization And Instantiation In C#

In this guide, we are going to explore the variance between Initialization and Instantiation in C#. Prior to delving into the variances, it is essential to comprehend Initialization and Instantiation in C# alongside their syntax and illustrations.

What is the Initialization?

In C#, initialization refers to the act of assigning initial values to variables. Before utilizing any variables, it is essential to initialize them with suitable 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 popular techniques for initializing variables include:

  • 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#, object instantiation involves creating an object or instance of a class within the program. During this process, memory is assigned to the objects, and the members of the object are initialized. The newly generated object has access to all the members and member functions within the class, including methods, operators, and fields.

Syntax for the instantiation:

It has the following syntax:

Example

objectName = new className(parameters);

Some common methods of Instantiation:

Several typical techniques for object creation are outlined below:

  • Utilizing the new keyword
  • Example
    
    MyClass obj = new MyClass();
    
  • Employing 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's consider a C# code example to demonstrate 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:

The given code snippet defines the styling for a placeholder diagram. It includes a background gradient, border radius, padding, margin, and text alignment properties. The diagram consists of an icon and text, each with specific styling rules.

Explanation:

In the program mentioned above, it consists of two classes: Main and Car. Within the Car class, there are three variables - two strings and one integer, which are set during the initialization in the constructor. Additionally, this class includes a display method that is responsible for showcasing the variables within the class. Moving on to the Main class, an object is instantiated and assigned specific values to the variables. The creation of this object is known as instantiation. Subsequently, utilizing the object, the DisplayDetails method is invoked to exhibit all the members within the class.

Difference between Initialization and Instantiation in C#

The <style> code snippet illustrates a CSS class for a placeholder diagram. This class sets the background with a linear gradient, border radius, padding, margin, and text alignment properties. Additionally, it includes styles for the placeholder icon and text within the diagram.

There are several distinctions between Initialization and Instantiation in C#. Some key variances between Initialization and Instantiation in C# are outlined below:

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: