Home  >  Article  >  Backend Development  >  How do arrays work in C#?

How do arrays work in C#?

WBOY
WBOYforward
2023-09-10 09:41:081304browse

数组在 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];

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#";

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

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

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#" };

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

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

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

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

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

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
}

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
}

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; }

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; }

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();

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; }

Output

csharp
3
csharp
visual basic
f#
csharp
visual basic
f#
0
0
0
0

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!

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