Topic 04 - Array & Collections
Topic 04 - Array & Collections
In C#, arrays are a fundamental data structure used to store a fixed-size sequence of
elements of the same type. Arrays are particularly useful when you need to manage
and manipulate collections of data that have a predictable and consistent size. Here’s a
detailed overview of arrays in C#, including their declaration, initialization, and
common operations.
1. Declaring Arrays
To declare an array in C#, you need to specify the type of its elements and use square
brackets `[]`. Here’s how you declare an array:
2. Initializing Arrays
After declaring an array, you need to initialize it to allocate memory. This can be done
in several ways:
5. Array Length
The `Length` property returns the number of elements in the array.
int[] numbers = { 1, 2, 3, 4, 5 };
int length = numbers.Length; // 5
7. Multidimensional Arrays
C# supports multidimensional arrays, such as two-dimensional arrays, which are
essentially arrays of arrays.
1. Two-Dimensional Array:
// Declaring and initializing a 2D array
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Accessing elements
int value = matrix[1, 2]; // 6 (second row, third column)
2. Jagged Arrays:
Jagged arrays are arrays of arrays and can have different lengths for each array.
// Declaring and initializing a jagged array
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2 };
jaggedArray[1] = new int[] { 3, 4, 5 };
jaggedArray[2] = new int[] { 6 };
// Accessing elements
int value = jaggedArray[1][2]; // 5
2. `Array.Reverse()`:
Reverses the order of elements in the array.
int[] numbers = { 1, 2, 3, 4, 5 };
Array.Reverse(numbers); // Array is now { 5, 4, 3, 2, 1 }
3. `Array.Copy()`:
Copies a range of elements from one array to another.
int[] source = { 1, 2, 3, 4, 5 };
int[] destination = new int[3];
Array.Copy(source, 1, destination, 0, 3); // destination is { 2, 3, 4 }
Collection
These collections are part of the System.Collections and System.Collections.Generic
namespaces. They include collections like lists, dictionaries, queues, and stacks.
1.1. List<T>
is a dynamic array that grows as needed. It provides fast indexed access to
List<T>
elements and supports a wide range of methods for manipulating data.
Example:
using System;using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
// Add an element
numbers.Add(6);
// Remove an element
numbers.Remove(10);
numbers.RemoveAt(0); // Remove by index
}
}
Exercise:
Write a C# program that takes an array of integers as input, reverses the order of
elements in the array, and prints the reversed array.
Write a C# program that removes all duplicates from a given array of integers and
prints the resulting array.
Write a C# program that finds and prints the maximum and minimum elements in
a given array of integers.
Write a C# program that counts the frequency of each element in an array of
integers and displays the result.