...

/

Storing Multiple Values in Array

Storing Multiple Values in Array

Learn how to work with arrays in C#, covering single-dimensional, multi-dimensional, jagged arrays, and list pattern matching techniques for array manipulation.

When we need to store multiple values of the same type, we can declare an array. For example, we may do this when storing four names in a string array.

Working with single-dimensional arrays

The code we will write will allocate memory for an array for storing four string values. It will then store string values at index positions 0 to 3 (arrays usually have a lower bound of zero, so the index of the last item is one less than the length of the array). We could visualize the array like this:

Press + to interact
Array Visualization
Array Visualization

Note: Do not assume that all arrays count from zero. The most common type of array in .NET is a szArray, a single-dimension zero-indexed array, and these use the normal [] syntax. But .NET also has mdArray, a multi-dimensional array, and they do not have to have a lower bound of zero. These are rarely used, but we should know they exist.

Finally, it will loop through each item in the array using a for statement. Let’s look at how to use an array:

Step 1: Use your preferred code editor to add a new "Console App/console" project named Arrays to the Chapter03 workspace/solution.

  • In Visual Studio Code, select Arrays as the active OmniSharp project. When you see the
    pop-up warning message that required assets are missing; click "Yes" to add them. ...