Working with Data in C#: Arrays
Originally published at https://allcoderthings.com/en/article/csharp-arrays In C#, arrays are used to store multiple values of the same type consecutively in memory. Indexing provides fast access t...

Source: DEV Community
Originally published at https://allcoderthings.com/en/article/csharp-arrays In C#, arrays are used to store multiple values of the same type consecutively in memory. Indexing provides fast access to elements. This article provides a detailed introduction from array declarations to multi-dimensional arrays, as well as practical examples using Array class methods. What is an Array? An array is a structure where data of the same type is stored in consecutive memory cells. Each element is accessed using an index number. Indexes start at 0. int[] numbers = new int[5]; // an int array with 5 elements Declaring Arrays and Default Values When declaring an array, its size is specified. If no values are assigned, default values are used: int → 0 bool → false string → null string[] names = new string[3]; Console.WriteLine(names[0]); // null Assigning and Accessing Array Elements (Indexing) Elements are accessed by index. Since indexes start at 0, the last element is located at Length - 1. int[] g