Arrays and bounds
An array stores a fixed number of elements of one declared type. Each element has an index starting at zero. You can replace an element without changing the array length; when the number of items must change frequently, consider a list.
using System;int[] counts = { 4, 7, 2 };Console.WriteLine(counts.Length);Console.WriteLine(counts[0]);Console.WriteLine(counts[counts.Length - 1]);Three elements occupy indices 0, 1 and 2.
Expected output (reasoned, not executed):
342Create and inspect an array
Use int[] counts = { 4, 7, 2 }; to initialize three integers. new int[3] creates three elements with value zero. For reference-type elements the default is null, so initialize references before using them. Length reports the number of elements. The last valid index is Length minus one, provided the array is not empty.
Traverse safely
In a for loop use i < counts.Length, not i <= counts.Length. A foreach loop is convenient when you only need values. For external indices, reject negative values and values at or above Length before accessing an element. An out-of-bounds array access throws IndexOutOfRangeException.
Assignment is not a copy
An array is a reference type: assigning one array variable to another copies the reference. Updating an element through either variable changes the same array. Clone() creates a shallow copy (element references would still be shared); for an integer array its independent slots contain independent integer values. The cast tells the compiler the returned object is an int array.
using System;int[] counts = { 4, 7, 2 };int total = 0;for (int i = 0; i < counts.Length; i++) total += counts[i];Console.WriteLine(total);Each valid index contributes once. With an empty array the loop would not run.
Expected output (reasoned, not executed):
13using System;int[] counts = { 4, 7 };int[] alias = counts;int[] copy = (int[])counts.Clone();alias[0] = 9;Console.WriteLine(counts[0]);Console.WriteLine(copy[0]);The alias shares the original array; the clone has separate integer slots.
Expected output (reasoned, not executed):
94Common mistake and correction
An index equal to Length is outside the array. For an empty array, Length is zero and there is no first or last item. Do not rely on an exception as the usual way to discover whether an index is valid. Guard access with index >= 0 && index < values.Length.
Try it yourself
For { 3, 0, 6 }, count positive elements and calculate the total using a loop. Repeat with new int[0] and { 0 }.
Next: Lists and collection expressions. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arrays and bounds — knowledge check
Apply the lesson concepts to the following scenarios.
What is the last valid index of a three-element array?
What is the initial value of new int[2] at index zero?
Which condition safely bounds an index i?
After int[] b = a, what does b[0] = 9 do?
What happens to the loop body with an empty array and i < a.Length?