Arrays are mutable
The elements of an array can be changed after the array is created. You assign to a slot with the same bracket notation you use to read it:
int[] ourArray = {50, 40, 30};
ourArray[0] = 15;
ourArray[1] = -1;
// ourArray is now {15, -1, 30}
Only the slots you assign to change. The other elements and the array's length stay the same. The length can't be changed at all, so you can replace what is stored in a slot but you can't add a fourth slot.
This differs from strings. A string in C# is immutable, so you can read word[0] but you can't assign to it. An array has no such restriction.
Compound assignment
An array element works like any other variable, so the compound operators work on it too. += reads the current value, adds to it and stores the result back in the same slot:
int[] scores = {10, 20, 30};
scores[2] += 5; // scores[2] is now 35
scores[0]++; // scores[0] is now 11
Arrays are shared, not copied
An array variable holds a reference to the array. Assigning it to another variable doesn't copy the elements, so a change made through one name is visible through the other:
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
Console.WriteLine(a[0]); // 99
Updating in a loop
Assignment and loops combine well. This loop adds 1 to every element of an array, one index at a time:
int[] ages = {14, 32, 50, 21};
for (int i = 0; i < ages.Length; i++) {
ages[i] += 1;
}
// ages is now {15, 33, 51, 22}
This is a case where foreach would not work. The loop variable of a foreach is a read-only copy of each value, so assigning to it is a compile error and can't change the array. When you need to change elements, loop over the indexes.
The exercise, step by step
- Replace the first fruit. The first item is at index 0, so write
fruits[0] = "apple";. Plain=overwrites"mango"with the new text. - Add 5 to the second age. The second item is at index 1, so write
ages[1] += 5;. The value 32 becomes 37.
string[] fruits = {"mango", "kiwi", "grapefruit"};
int[] ages = {14, 32, 50, 21};
fruits[0] = "apple";
ages[1] += 5;
After these lines fruits holds apple, kiwi, grapefruit, and ages holds 14, 37, 50, 21.
The same idea applies to every update. ages[1] += 5 reads slot 1, computes the sum and writes it back, so the previous value of 32 is replaced by 37. If you still need 32 afterwards, copy it to a variable before you update the slot.
Each assignment touches exactly one position, so the cost of an update does not depend on the array's size.
Common mistakes
- Assigning past the end.
ages[4] = 1;on a four-element array throws anIndexOutOfRangeException. Assigning never makes an array bigger. - Assigning the wrong type.
ages[0] = "ten";doesn't compile, because the array holds ints. - Mixing up
=and+=.ages[1] = 5;sets the slot to 5, whileages[1] += 5;adds 5 to what is there. - Forgetting the index.
fruits = "apple";tries to assign a string to the whole array variable, and doesn't compile.
What happens to the old value
Assignment replaces the stored value, and the old one is gone unless you saved it first. To swap two elements you need a temporary variable, because writing the first slot would destroy the value you still need:
int[] pair = {1, 2};
int temp = pair[0];
pair[0] = pair[1];
pair[1] = temp;
// pair is now {2, 1}
Things to try
After the exercise, try copying one element onto another with ages[0] = ages[3];. The array then reads 21, 37, 50, 21 when you run it after the earlier update, because the right side is evaluated first and its value is stored in slot 0. Elements of the same array can be freely combined in this way. Printing the array with string.Join(", ", ages) is a handy way to see all the elements at once.
Key takeaways
- Array elements are mutable:
array[i] = value;replaces one slot. - Compound operators such as
+=work on elements. - The length never changes, and copying an array variable shares the same array.