Home > Article > Backend Development > What is the difference between initialization and assignment in C#?
Let us understand the difference between initialization and value assignment.
Declare an array.
int [] n // declaring
Declaring an array does not initialize the array in memory. After the array variable is initialized, you can assign a value to the array. Arrays are reference types, so you need to use the new keyword to create an instance of the array.
int n= new int[10]; // initialization
Let's assign values. You can assign values to individual array elements using index numbers -
n[0] = 100; n[1] = 200
With C# you can declare, initialize and assign values to an array in one line -
int n= new int[10] {100, 200, 300, 400, 500};
When creating an array, the C# compiler will implicitly initialize each array element to a default value based on the array type. For example, for an int array, all elements are initialized to 0.
The above is the detailed content of What is the difference between initialization and assignment in C#?. For more information, please follow other related articles on the PHP Chinese website!