C# & .NET Framework Arrays & String Mastery
💡
Exercise 26

Arrays — The Material Warehouse 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

📦 The Architect's Storage Warehouse

Every great construction project begins in the warehouse — a place where building materials are organized, catalogued, and retrieved on demand. In C#, the array is your warehouse: a fixed-size, contiguous block of memory that stores elements of the same type, each accessible by a numeric index.

Arrays are one of the oldest and most fundamental data structures in computing. The CLR (Common Language Runtime) allocates array memory on the managed heap, and every array in .NET inherits from System.Array, giving you a rich set of built-in methods right out of the box.

🏗️ Declaring and Initializing Arrays

There are several ways to create an array in C#. Each approach has its place depending on whether you know the values upfront or just the size you need.

// Method 1: Declare with a fixed size (elements default to 0 for int) int[] slots = new int[5]; // Method 2: Declare and initialize with values string[] materials = new string[] { "Steel", "Wood", "Glass" }; // Method 3: Shorthand — compiler infers the type and size string[] tools = { "Hammer", "Drill", "Saw" }; // Method 4: Using var with explicit new var beams = new double[] { 12.5, 8.0, 15.3 };

🔑 Accessing Elements by Index

Array indices in C# are zero-based. The first element lives at index 0, and the last element lives at index Length - 1. Attempting to access an index outside this range throws an IndexOutOfRangeException — the CLR performs bounds checking on every access to keep your program safe.

string[] materials = { "Steel", "Wood", "Glass", "Concrete", "Marble" }; // Access first and last Console.WriteLine(materials[0]); // Steel Console.WriteLine(materials[materials.Length - 1]); // Marble // Modify an element materials[1] = "Titanium"; Console.WriteLine(materials[1]); // Titanium

📏 The Length Property

Every array has a Length property that returns the total number of elements. This is a read-only property — you cannot resize an array after creation. If you need a resizable collection, you'll eventually reach for List<T>, but for now, fixed-size arrays are our warehouse shelves.

🔄 Iterating Over Arrays

You can loop through arrays using for (when you need the index) or foreach (when you just need each value).

string[] materials = { "Steel", "Wood", "Glass" }; // for loop — gives you the index for (int i = 0; i < materials.Length; i++) { Console.WriteLine($"Slot {i}: {materials[i]}"); } // foreach — cleaner when you don't need the index foreach (string m in materials) { Console.WriteLine(m); }

🛠️ Useful Array Methods

The static Array class provides powerful utility methods:

  • Array.Sort(arr) — Sorts elements in ascending order (uses introspective sort, O(n log n) average).
  • Array.Reverse(arr) — Reverses the order of elements in place.
  • Array.IndexOf(arr, value) — Returns the index of the first occurrence, or -1 if not found.
  • Array.Copy(source, dest, length) — Copies elements from one array to another.
  • Array.Clear(arr, index, length) — Sets a range of elements to their default value.
  • Array.Exists(arr, predicate) — Returns true if any element matches a condition.
  • Array.Find(arr, predicate) — Returns the first element matching a condition.
string[] materials = { "Glass", "Steel", "Wood" }; Array.Sort(materials); // materials is now: { "Glass", "Steel", "Wood" } Array.Reverse(materials); // materials is now: { "Wood", "Steel", "Glass" } int idx = Array.IndexOf(materials, "Steel"); Console.WriteLine(idx); // 1

⚡ Key Takeaways

  • Arrays are fixed-size — once created, their length cannot change.
  • Indices are zero-based (0 to Length - 1).
  • All elements must be the same type (or a compatible subtype for reference types).
  • Arrays are reference types — assigning one array variable to another copies the reference, not the data.
  • The Array class provides static methods for sorting, searching, copying, and more.
📋 Instructions
## 🏗️ Warehouse Inventory Challenge The architect needs you to set up the material warehouse for project **Skyline Tower**. 1. Create a `string` array called `materials` with these 5 items: `"Steel"`, `"Wood"`, `"Glass"`, `"Concrete"`, `"Marble"` 2. Print all materials on one line separated by `", "` using `String.Join()`, prefixed with `"Materials: "` 3. Print the count: `"Count: 5"` 4. Print the first element: `"First: Steel"` 5. Print the last element (use `Length - 1`): `"Last: Marble"` 6. Sort the array using `Array.Sort()`, then print sorted materials on one line separated by `", "`, prefixed with `"Sorted: "`
Use `new string[] { ... }` or just `{ ... }` to create the array. `String.Join(", ", materials)` joins all elements. Access the last element with `materials[materials.Length - 1]`. Call `Array.Sort(materials)` before printing the sorted line.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.