Javatpoint Logo
Javatpoint Logo

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:

Some common methods for initialization:

Several common method for initializations are as follows:

  • Constructor Initialization
  • Object Initialization
  • Collection Initialization
  • Array Initialization

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:

Some common methods of Instantiation:

Several common method for instantiation are as follows:

  • Using new keyword
  • Using clone or copy constructors
  • Using Reflection

Example:

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

Output:

Difference between Initialization and Instantiation in C#

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#

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= 5
Immutable: string s= "javaTpoint"
It creates mutable objects or instances
Mutable
MyClass 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 constructors
Example:
static void Main()
{
// Accessing class-scoped method
MyClass.ClassMethod();
}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA