Arrays And Collections
Lesson 2.5: Arrays and Collections: Storing Multiple Items
Up until now, you've worked with variables that hold a single value at a time. In this lesson, you'll learn about two powerful ways to store collections of data: arrays and lists.
Arrays: Fixed-Size Collections
What Are They? An array is a container that can hold a fixed number of elements of the same data type. Think of it like a row of boxes, where each box can store one item (e.g., numbers, strings, or other types).
Why Use Arrays?
Organized Storage: Group related data together.
Efficient Access: Quickly access elements by their index (position).
Perform Operations: Easily sort, search, or manipulate the entire collection.
Declaring an Array:
C#
int[] numbers = new int[5]; // Creates an array to hold 5 integers
string[] names = new string[3]; // Creates an array to hold 3 strings
Initializing an Array:
C#
int[] numbers = new int[] { 5, 10, 15, 20, 25 }; // Initialization at declaration
string[] names = { "Alice", "Bob", "Charlie" }; // Shorter initialization syntax
Use code with caution.
content_copy
Accessing Array Elements:
Use square brackets [] to access elements by their index (starting from 0):
C#
numbers[0] = 12; // Set the first element (index 0) to 12
string secondName = names[1]; // Get the second element (index 1)
Lists: Flexible Collections
What Are They? Lists are similar to arrays, but they can grow or shrink in size dynamically. This makes them more flexible when you don't know how many elements you'll need.
Why Use Lists?
Dynamic Size: Add or remove elements easily.
Built-in Methods: Many helpful methods for sorting, searching, inserting, and removing elements.
Declaring and Initializing a List:
C#
using System.Collections.Generic; // Required for using lists
List<int> numbers = new List<int>(); // Create an empty list of integers
numbers.Add(5); // Add elements to the list
numbers.Add(10);
Accessing List Elements:
Similar to arrays, use square brackets [] to access elements by index:
C#
int firstNumber = numbers[0];
Important Considerations
Array Size: Arrays have a fixed size, determined when you create them. You can't change the size later.
List Type: When creating a list, specify the data type of the elements it will hold (e.g., List<int> for integers).
Index Out of Range: Be careful not to try accessing elements at invalid indices (beyond the size of the array or list).
Example: Working with Arrays and Lists
C#
int[] ages = { 25, 30, 35 };
List<string> fruits = new List<string> { "apple", "banana", "orange" };
Console.WriteLine(ages[1]); // Output: 30
Console.WriteLine(fruits[2]); // Output: orange
fruits.Add("grape"); // Add a new fruit to the list
fruits.Remove("banana"); // Remove "banana" from the list
Going Further
Arrays and lists are just the tip of the iceberg. C# offers many other collection types for different use cases, such as:
Dictionary<TKey, TValue>: Store key-value pairs.
HashSet<T>: Store unique elements.
Queue<T> and Stack<T>: Implement specific data structures.
As you continue your C# journey, you'll discover how these powerful collections can help you solve a wide range of programming challenges!