Working with Collections and Arrays in C#

Introduction

C# provides powerful ways to store and manage collections of data. Two fundamental approaches are using arrays and employing the more flexible List collection. This tutorial will guide you through both, explaining their characteristics and demonstrating how to add values to them.

Arrays in C#

An array is a fixed-size, contiguous block of memory that holds elements of the same data type. When you declare an array, you must specify its size upfront.

Declaring and Initializing an Array:

int[] numbers = new int[10]; // Creates an array of 10 integers, initialized to default values (0 for int)

Adding Values to an Array:

You access array elements using their index (starting from 0). To add a value, you assign it to a specific index.

numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
// ... and so on

Example: Populating an Array with a Loop:

int[] terms = new int[400];
for (int i = 0; i < 400; i++)
{
    terms[i] = i; // Assigns the value of 'i' to each element of the array
}

Important Considerations:

  • Arrays have a fixed size. Once created, you cannot change their size without creating a new array and copying the contents.
  • Accessing an index outside the array’s bounds will result in an IndexOutOfRangeException.

Lists in C#

The List<T> collection (from the System.Collections.Generic namespace) provides a dynamic alternative to arrays. List<T> automatically manages its size, growing or shrinking as needed. The T represents the type of data the list will hold (e.g., List<int>, List<string>).

Creating a List:

using System.Collections.Generic;

List<int> numbers = new List<int>(); // Creates an empty list of integers

Adding Values to a List:

The Add() method is used to add elements to the end of a list.

numbers.Add(10);
numbers.Add(20);
numbers.Add(30);

Example: Populating a List with a Loop:

List<int> terms = new List<int>();
for (int i = 0; i < 400; i++)
{
    terms.Add(i); // Adds the value of 'i' to the end of the list
}

Converting a List to an Array:

If you need an array after working with a list, you can use the ToArray() method.

int[] termsArray = terms.ToArray();

Benefits of Using Lists:

  • Dynamic Size: Lists automatically adjust their size, eliminating the need to specify the size upfront.
  • Convenience: Add(), Remove(), and other methods simplify common collection operations.
  • Flexibility: Lists are generally more flexible than arrays for many scenarios.

Choosing Between Arrays and Lists

  • Use arrays when you know the size of the collection in advance and need a fixed-size, high-performance data structure.
  • Use lists when the size of the collection is dynamic or unknown, or when you need the convenience of built-in methods for adding, removing, and managing elements.

Modern Approaches

In newer versions of C#, you can leverage LINQ for concise collection manipulation. For example, to create an array of 400 integers using LINQ:

using System.Linq;

int[] terms = Enumerable.Range(0, 400).ToArray();

Similarly, to create a list:

List<int> termsList = Enumerable.Range(0, 400).ToList();

These approaches provide a declarative way to define collections and are often more readable and maintainable.

Leave a Reply

Your email address will not be published. Required fields are marked *