How do arrays work in C#?

WBOY
Release: 2023-09-10 09:41:08
forward
1303 people have browsed it

数组在 C# 中如何工作?

An array represents a fixed number of elements of a given type. Elements are stored in contiguous blocks of memory, and you can access elements efficiently as long as you know their index.

The C# syntax for declaring and initializing an array is as follows -

// create a string array to hold 5 languages
string[] languages = new string[3];
Copy after login

After declaring an array, you can use the square symbol on the array to fill the items in the array.

languages[0] = "csharp";
languages[1] = "visual basic";
languages[2] = "f#";
Copy after login

However, there is an alternative concise syntax that combines the above two steps in one step.

string[] languages = new string[] { "csharp", "visual basic", "f#" };
Copy after login

With implicitly typed variables in C#, you can remove the type on the left side of the variable name and just use var.

var languages = new string[] { "csharp", "visual basic", "f#" };
Copy after login

You can access any element in the array using the same square notation.

string language = languages[0];
Console.WriteLine(language); // prints csharp
Copy after login

The Length property of an array tells us how many items are in the array.

int count = languages.Length;
Console.WriteLine(count); // 3
Copy after login

It is important to remember that the length of an array cannot be changed once it is created. Attempting to access an element outside the bounds of the array throws an exception.

string lang = languages[3]; // throws System.IndexOutOfRangeException
Copy after login

If you want to loop over an array, use the for or foreach loop provided by the language.

// for loop
for (int i = 0; i < languages.Length; i++){
   string l = languages[i];
   Console.WriteLine(l); // prints the names of languages in sequence
}
// foreach loop
foreach (string l in languages){
   Console.WriteLine(l); // prints the names of languages in sequence
}
Copy after login

Default value

When you create an array, the C# compiler initializes the elements to their default values. For example, creating an array of integers sets the value of each element to 0.

var numbers = new int[3];
foreach (int num in numbers){
   Console.WriteLine(num); // prints 000
}
Copy after login

When an element is of a value type, each member of that type is assigned its default value.

Point[] a = new Point[4];
int x = a[2].X;
Console.WriteLine(x); // prints 0
public struct Point { public int X, Y; }
Copy after login

However, for reference types, the compiler just creates a null reference for each item.

User[] u = new User[4];
int y = u[2].Y; // NullReference exception
public class User { public int X, Y; }
Copy after login

To avoid this error, you must explicitly initialize each item as shown below.

User[] users = new User[5];
for (int i = 0; i < users.Length; i++) // Iterate i from 0 to 999
users[i] = new User();
Copy after login

Example

Real-time demonstration

using System;
class Program{
   static void Main(){
      string[] languages = new string[3]; // create a string array to hold 5 languages
      languages[0] = "csharp";
      languages[1] = "visual basic";
      languages[2] = "f#";
      string[] langugages_two = new string[] { "csharp", "visual basic", "f#" };
      var languages_three = new string[] { "csharp", "visual basic", "f#" };
      string language = languages[0];
      Console.WriteLine(language); // prints csharp
      int count = languages.Length; // 3
      Console.WriteLine(count);
      //string lang = languages[3]; // throws System.IndexOutOfRangeException
      // for loop
      for (int i = 0; i < languages.Length; i++){
         string l = languages[i];
         Console.WriteLine(l); // prints the names of languages in sequence
      }
      // foreach loop
      foreach (string l in languages){
         Console.WriteLine(l); // prints the names of languages in sequence
      }
      var numbers = new int[3];
      foreach (int num in numbers){
         Console.WriteLine(num); // prints 000
      }
      Point[] a = new Point[4];
      int x = a[2].X;
      Console.WriteLine(x); // prints 0
      User[] u = new User[4];
      //int y = u[2].Y; // NullReference exception
      User[] users = new User[5];
      for (int i = 0; i <users.Length; i++) // Iterate i from 0 to 999
      users[i] = new User();
   }
}
public struct Point { public int X, Y; }
public class User { public int X, Y; }
Copy after login

Output

csharp
3
csharp
visual basic
f#
csharp
visual basic
f#
0
0
0
0
Copy after login

The above is the detailed content of How do arrays work 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!