How do arrays work in C#?

CsharpServer Side ProgrammingProgramming

An array represents a fixed number of elements of a given type. The elements are stored in a contiguous block of memory and provide highly efficient access to the elements as long as you know the index of an element.

The C# syntax to declare and initialize an array is as follows −

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

Once the array is declared, you can populate the items in the array by using the square notation on the array.

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

However, there is an alternate, concise syntax to combine the above two steps in a single step.

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

With the implicitly typed variables in C#, you can get rid of the type on the left-hand 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 on an array tells us how many items are in that array.

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

It's important to remember that you can't change the length of an array once it's created. Trying to access an element outside the bounds of an 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 Values

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 the element belongs to a value type, each member of the type is assigned to 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 simply creates null references for each item.

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

To avoid the error, you must initialize each item explicitly, as follows.

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

Example

 Live Demo

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
raja
Published on 19-May-2021 07:51:18
Advertisements