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

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

PHPz
Release: 2023-09-04 13:05:02
forward
1521 people have browsed it

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

Let us understand the difference between initialization and value assignment.

Declare an array.

int []  n  // declaring
Copy after login

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
Copy after login

Let's assign values. You can assign values ​​to individual array elements using index numbers -

n[0] = 100;
n[1] = 200
Copy after login

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};
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template