Home  >  Article  >  Backend Development  >  What is the difference between initialization and assignment in C#?

What is the difference between initialization and assignment in C#?

PHPz
PHPzforward
2023-09-04 13:05:021476browse

C# 中的初始化和赋值有什么区别?

Let us understand the difference between initialization and value assignment.

Declare an array.

int []  n  // declaring

initialization

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete