C# Arrays and Jagged Arrays — Complete Beginner Guide
Learning Objectives
- Declare and initialize one dimensional arrays
- Read and update values by index
- Loop over arrays safely
- Build jagged arrays of arrays
- Choose arrays versus other collections
What Is an Array
An array stores multiple values of the same type in one variable. Each element has an index starting at zero. Arrays are fixed in length once created.
int[] scores = new int[3];
scores[0] = 90;
scores[1] = 85;
scores[2] = 78;
Console.WriteLine(scores[1]);
The expression scores one is out of range because valid indexes are zero, one, and two.
Initializing with Values
You can fill an array when you declare it.
string[] names = { "Asha", "Ben", "Cara" };
double[] prices = new double[] { 9.99, 14.5, 3.25 };
Console.WriteLine(names[0]);
Console.WriteLine(prices.Length);
Length reports how many elements the array holds, not the last index.
Looping Over Arrays
for loop
int[] nums = { 1, 2, 3, 4 };
for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}
foreach loop
foreach (int n in nums)
{
Console.WriteLine(n);
}
Use for when you need the index. Use foreach when you only need each value.
Updating Elements
Arrays are mutable. Assign new values any time.
int[] cart = { 10, 20, 30 };
cart[1] = 25;
Console.WriteLine(cart[1]);
Array Properties and Tricks
int[] data = { 5, 9, 2 };
Console.WriteLine(data.Length);
Console.WriteLine(data[0]);
Common patterns include finding a maximum, summing values, or searching for a target.
int[] values = { 4, 17, 8 };
int max = values[0];
for (int i = 1; i < values.Length; i++)
{
if (values[i] > max)
{
max = values[i];
}
}
Console.WriteLine(max);
Two Dimensional Arrays
Rectangular matrices use a comma inside the brackets.
int[,] grid = new int[2, 3];
grid[0, 0] = 1;
grid[1, 2] = 6;
Console.WriteLine(grid[1, 2]);
Rows and columns share one fixed rectangle shape.
Jagged Arrays
A jagged array is an array whose rows can have different lengths. Think of it as an array of arrays.
int[][] jagged = new int[3][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
jagged[2] = new int[] { 6 };
Accessing jagged values
Console.WriteLine(jagged[1][2]);
Console.WriteLine(jagged[0].Length);
The first set of brackets picks the row. The second picks the column inside that row.
Building jagged data in loops
int[][] table = new int[3][];
for (int r = 0; r < table.Length; r++)
{
table[r] = new int[r + 1];
for (int c = 0; c < table[r].Length; c++)
{
table[r][c] = r + c;
Console.Write(table[r][c] + " ");
}
Console.WriteLine();
}
Each row grows longer, which jagged arrays allow naturally.
Rectangular vs Jagged
| Feature | Rectangular | Jagged |
|---|---|---|
| Syntax | int with comma | Array of arrays |
| Row lengths | Equal | Independent |
| Memory style | Single block conceptually | One array per row |
| Best for | Fixed grids | Ragged data |
Common Mistakes
Off by one index
int[] a = { 1, 2, 3 };
// Console.WriteLine(a[3]); // Invalid
Console.WriteLine(a[a.Length - 1]);
Forgetting to create inner arrays
int[][] j = new int[2][];
// j[0][0] = 1; // Error: j[0] is null
j[0] = new int[1];
j[0][0] = 1;
Confusing Length with last index
Last index is always Length minus one.
Key Takeaways
💡
- Arrays hold same typed values at zero based indexes
- Length returns element count, not the final index
- foreach is clean for value only walks; for gives indexes
- Jagged arrays are arrays of arrays with independent row sizes
- Always create inner arrays before assigning jagged elements
Practice Questions
- What index does the first element of an array use?
- How do you print the number of elements in scores?
- Write a for loop that prints every value in an int array.
- How is a jagged array different from a rectangular two dimensional array?
- Create a jagged array with rows of lengths two, three, and one, then print the middle value of the second row.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions