Lesson Weekend

As we learned in Intro, collections like arrays are an important part of programming. We can group things together, loop through them, and manipulate large amounts of data with arrays.

C# arrays work very differently than the JavaScript arrays we've used. Let's explore how they're different.

Array vs. List


JavaScript arrays are flexible. They can contain a variety of different things and their size automatically fluctuates when items are added or removed.

On the other hand, C# includes both arrays and lists. They're each collections of multiple items, but are very different:

  • A list is similar to arrays in JavaScript. However, they generally only hold one type of data (like string or int). They fluctuate in size as items are added or removed.

  • An array is stricter and has a set length. After an array is created, we cannot add or remove objects to make it larger or smaller. All items stored in an array must be the same data type (such as string or int).

This lesson will focus solely on C# arrays. We'll explore lists more closely in the next lesson.

Arrays


We can create an array containing strings in our REPL like this:

> string[] fruits = { "apples", "bananas", "oranges", "grapes" };

> fruits
{"apples", "bananas", "oranges", "grapes"}

We can create an array of integers like this:

> int[] primeNumbers = { 2, 3, 5, 7, 11 };
> primeNumbers
{2, 3, 5, 7, 11}

There are several steps to creating a C# array:

  1. Determine what type of data the array contains. Data in C# arrays must be the same type. To define an array, we must first determine what data type it will hold. In the examples above, we state string when creating an array of fruits and int when creating an array of numbers.

  2. Append square brackets [] to the data type. These allow us to declare that the object we're about to create will be an array. In the example above, int[] means an array of integers. string[] represents an array of strings. There should be no spaces between the data type and square brackets.

  3. Include a variable name. After declaring the array and type of data it holds, include a name. In the examples above, we used string[] fruits and int[] primeNumbers.

  4. Assign data to the array. We use the assignment operator = to assign the array variable a list of information. Note this is inside {}, or curly brackets, not the square brackets we used for JavaScript arrays. Remember that the length of the array may not change after you assign it.

Accessing Array Data

We can access an array by calling the array's variable name. For instance, in the example below, calling fruits returns the full array:

> string[] fruits = { "apples", "bananas", "oranges", "grapes" };
> fruits
{"apples", "bananas", "oranges", "grapes"}

To access a specific entry in the array, we append square brackets [] to the array's variable name. Inside the brackets we provide the index of the item we want:

> fruits[1]
"bananas"

> fruits[3]
"grapes"

> string favoriteFruit = fruits[0];
> favoriteFruit
"apples"

Remember that items in a list or array should always start at 0. The first item in an array is actually at index 0, the second is at index 1, and the one hundredth is at index 99.

Altering Array Data

We cannot change an array's length but we can change values inside it. For instance, we know the item in the 2nd index of our array is "oranges":

> fruits[2]
"oranges"

Let's say we want to be more specific and change this value to "blood oranges". We can redefine the item at that index like this:

> fruits[2] = "blood oranges";

> fruits
{"apples", "bananas", "blood oranges", "grapes"}

We already know we can call fruits[2] to access the second index of our fruits array. This time we just include the = operator to assign a new value.

This can be done for any index of the array:

> fruits[3] = "concord grapes";

> fruits
{"apples", "bananas", "blood oranges", "concord grapes"}

In the next lesson, we'll learn more about C# lists.

Terminology


Array: has a set length in C#. We cannot add or remove objects from a C# array.

List: has a variable length. We can add or remove objects from a C# list.

Examples


We can create an array like this:

> int[] primeNumbers = { 2, 3, 5, 7, 11 };
> primeNumbers;
{2, 3, 5, 7, 11}
  1. Determine what type of data the array contains. Arrays can only hold one type of data.

  2. Append square brackets [] to the data type when making a type declaration.

  3. Include a variable name.

  4. Assign data to the array. We use the assignment operator = to assign the array variable a list of information. Curly brackets, or {}, are used for C# arrays.

Accessing Array Data


To access the entire array:

> string[] fruits = { "apples", "bananas", "oranges", "grapes" };
> fruits;
{"apples", "bananas", "oranges", "grapes"}

Use square brackets containing the index to access a specific entry:

> fruits[1];
"bananas"

> fruits[3];
"grapes"

> string favoriteFruit = fruits[0];
> favoriteFruit;
"apples"

Update a value inside an array:

> fruits[2] = "blood oranges";

Lesson 4 of 9
Last updated more than 3 months ago.