Cross_Column

Wednesday, 1 July 2026

Arrays in C#



Arrays in C# | Beginner Tutorial

Arrays in C#

Learn how to create, initialize, access, and iterate through arrays in C# with beginner-friendly examples.

Table of Contents

  1. What is an Array?
  2. Why Use Arrays?
  3. Declare an Array
  4. Initialize an Array
  5. Access Array Elements
  6. Loop Through an Array
  7. Multidimensional Arrays
  8. Useful Array Properties
  9. Best Practices
  10. Summary

What is an Array?

An array is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow you to store multiple values in a single variable instead of creating separate variables for each value.

Note: Array indexes always start from 0.

Why Use Arrays?

  • Store multiple values efficiently.
  • Reduce code duplication.
  • Process data using loops.
  • Improve code organization.

Declaring an Array

int[] numbers;

This creates an array variable that can store integer values.

Initializing an Array

int[] numbers = {10, 20, 30, 40, 50};

You can also specify the size explicitly.

int[] numbers = new int[5];

Accessing Array Elements

int[] numbers = {10,20,30,40};

Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[2]);

Output

10
30
Common Mistake: Accessing an invalid index (such as numbers[5]) throws an IndexOutOfRangeException.

Looping Through an Array

Using for Loop

int[] numbers = {10,20,30,40};

for(int i=0;i<numbers.Length;i++)
{
    Console.WriteLine(numbers[i]);
}

Using foreach Loop

foreach(int number in numbers)
{
    Console.WriteLine(number);
}
Tip: Use foreach when you only need to read array values.

Multidimensional Array

int[,] matrix =
{
    {1,2},
    {3,4}
};

Console.WriteLine(matrix[0,1]);

Output

2

Useful Array Properties

Property / Method Description
Length Returns the total number of elements.
Sort() Sorts the array.
Reverse() Reverses the array.
IndexOf() Returns the index of an element.
Copy() Copies one array into another.

Complete Example

using System;

class Program
{
    static void Main()
    {
        string[] fruits =
        {
            "Apple",
            "Mango",
            "Orange",
            "Banana"
        };

        foreach(string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }

        Console.WriteLine("Total Fruits: " + fruits.Length);
    }
}

Best Practices

  • Choose meaningful array names.
  • Always check array indexes.
  • Use foreach for read-only iteration.
  • Use Length instead of hardcoding the array size.
  • Use collections like List<T> when the size changes frequently.

Summary

ConceptDescription
ArrayStores multiple values of the same type.
IndexStarts from 0.
LengthReturns number of elements.
forAccess elements using indexes.
foreachIterate through all elements.

Interview Questions

  1. What is an array?
  2. Why do array indexes start at 0?
  3. What is the difference between for and foreach?
  4. What does the Length property return?
  5. What exception occurs when accessing an invalid index?

Practice Exercise

  • Create an array of 5 student names.
  • Print all elements using a for loop.
  • Print all elements using a foreach loop.
  • Find the total number of elements using Length.
  • Create and print a 2×2 multidimensional array.

FAQ

Can an array store different data types?
No. All elements must be of the same data type.

Can the size of an array change?
No. Arrays have a fixed size after creation. Use List<T> if you need a dynamic collection.


Next Tutorial: Methods in C#

No comments:

Post a Comment

Few More

Recursion in C#

Recursion in C# | Beginner Tutorial Recursion in C# Learn how recursion works in C#, understand the base case and re...