🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

C# Arrays and Jagged Arrays — Complete Beginner Guide

C# FundamentalsArrays and Jagged Arrays🟢 Free Lesson

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

FeatureRectangularJagged
Syntaxint with commaArray of arrays
Row lengthsEqualIndependent
Memory styleSingle block conceptuallyOne array per row
Best forFixed gridsRagged 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

  1. What index does the first element of an array use?
  2. How do you print the number of elements in scores?
  3. Write a for loop that prints every value in an int array.
  4. How is a jagged array different from a rectangular two dimensional array?
  5. 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

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. 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

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. 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

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids 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
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert C# Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement