An index identifies a position, not an item’s meaning
An array stores a fixed number of element positions. C# uses zero-based indexing, so an array of three names has valid indexes zero, one, and two. The index identifies the position in this particular array; it is not a permanent identity for the person stored there. If the array is reordered or an element is replaced, the same numeric position can refer to a different name. Keep that distinction in mind before using an index as a business identifier.
The declaration infers a string array because all three elements are strings. Reading an element obtains the value stored at that position. A string is an immutable reference type, so assigning an element to another variable does not clone its characters or make a second mutable name object. It gives the new variable the same string value/reference. Later replacing the array element is a different operation from modifying the original string, which strings do not allow in place.
Read the second position and preserve the array
The initializer places Ada at index zero, Alex at index one, and Sam at index two. names[1] therefore evaluates to Alex. That result is assigned to chosen, and WriteLine prints Alex. The array still contains all three names; lookup does not remove the selected element, shift positions, or advance a hidden cursor. Repeating the same lookup without changing the array returns the same value again, because this array is not an input stream.
The original sample contains one output call and consequently one visible output line. The brackets perform the indexed read before WriteLine receives the string. If you instead request index three, the program can compile but the access is outside the array bounds at runtime. That is an exception rather than a blank string or a missing-name placeholder. A collection lookup contract must decide whether invalid positions are programmer errors or expected input that should be validated before access.
var names = new[] { "Ada", "Alex", "Sam" };
var chosen = names[1];
Console.WriteLine(chosen);Length is a count, not the final valid index
The last ordinary index of a nonempty array is Length minus one. Using Length directly accesses the position after the final element. Subtracting one is still invalid when the array is empty, so a last-item operation needs a nonempty precondition. Also do not assume that a position named chosen will keep tracking that slot: chosen is a separate variable, not an alias to the array element. Replacing names at index one later does not automatically replace the string stored in chosen.
Change a position, then replace its contents
First change the requested index to zero and predict Ada, then to two and predict Sam. Restore one before continuing. Add a line that prints names.Length and confirm the count remains three after the lookup. Hint: a successful indexed read neither consumes an element nor changes the array's shape. Test an invalid index only as a deliberate failure exercise, and distinguish its runtime exception from the compiler diagnostics used in the previous course.
Next retain var chosen = names[1], replace that array slot with Lin, and print both chosen and names at index one. Predict Alex followed by Lin. Hint: draw two variable slots initially referring to the Alex string, then change only the array slot's reference. Finally write a safe position-checking branch for an index supplied as data, requiring both a nonnegative value and a value less than Length. Test negative one, zero, the last index, and Length.
State the valid domain of the lookup
Describe the relationship between array length, valid indexes, and the selected value before running a new example. A useful explanation identifies whether the operation reads, replaces, or removes an item rather than using lookup as a vague word for all three. For this sample, the key observation is Alex with three names still present. For a larger model, decide whether a stable identifier is needed instead of a position that can change with ordering.
- Three elements have ordinary indexes zero through two.
- The unchanged lookup prints
Alexand removes nothing. - A position equal to Length is outside the array.
- Replacing an array slot does not make chosen track that slot.
A working example
var names = new[] { "Ada", "Alex", "Sam" };
var chosen = names[1];
Console.WriteLine(chosen);Expected output
Alex
A lookup does not change the list.