0
0
C Sharp (C#)programming~5 mins

Why collections over arrays in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Collections are more flexible than arrays. They can grow or shrink in size easily, while arrays have a fixed size.

When you don't know how many items you will need to store in advance.
When you want to add or remove items frequently.
When you want to use built-in methods to search, sort, or manipulate data easily.
When you want safer code that checks for errors like going out of bounds.
When you want to store different types of objects in one place using generic collections.
Syntax
C Sharp (C#)
using System.Collections.Generic;

// Example of a collection: List<T>
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Remove(10);

// Example of an array
int[] fixedNumbers = new int[5];
fixedNumbers[0] = 10;

Collections like List can change size dynamically.

Arrays have a fixed size set when created.

Examples
Adding items to a collection and checking its size.
C Sharp (C#)
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
Console.WriteLine(fruits.Count);  // Output: 2
Trying to add beyond array size causes an error.
C Sharp (C#)
int[] numbers = new int[3];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
// numbers[3] = 4; // This will cause an error because array size is fixed
An empty collection starts with zero items.
C Sharp (C#)
List<int> emptyList = new List<int>();
Console.WriteLine(emptyList.Count);  // Output: 0
An array with one element.
C Sharp (C#)
int[] singleElementArray = new int[1] { 42 };
Console.WriteLine(singleElementArray[0]);  // Output: 42
Sample Program

This program shows how arrays have a fixed size and cannot add new items, while collections like List can add and remove items easily.

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Create an array with fixed size
        int[] fixedArray = new int[3] { 1, 2, 3 };
        Console.WriteLine("Array contents before:");
        foreach (int number in fixedArray)
        {
            Console.Write(number + " ");
        }
        Console.WriteLine();

        // Create a collection (List) that can grow
        List<int> dynamicList = new List<int> { 1, 2, 3 };
        Console.WriteLine("List contents before adding:");
        foreach (int number in dynamicList)
        {
            Console.Write(number + " ");
        }
        Console.WriteLine();

        // Try to add an item to the array (not possible)
        // fixedArray[3] = 4; // This would cause an error

        // Add an item to the list
        dynamicList.Add(4);
        Console.WriteLine("List contents after adding 4:");
        foreach (int number in dynamicList)
        {
            Console.Write(number + " ");
        }
        Console.WriteLine();

        // Remove an item from the list
        dynamicList.Remove(2);
        Console.WriteLine("List contents after removing 2:");
        foreach (int number in dynamicList)
        {
            Console.Write(number + " ");
        }
        Console.WriteLine();
    }
}
OutputSuccess
Important Notes

Adding or removing items in a List is usually fast but can take more time if the internal array needs to resize.

Arrays use less memory and are faster for fixed-size data.

Common mistake: Trying to add items to an array after creation causes errors.

Use arrays when size is fixed and performance is critical; use collections when you need flexibility.

Summary

Collections can grow and shrink; arrays cannot.

Collections provide useful methods to manage data easily.

Choose collections for flexible data and arrays for fixed-size data.