Array class in C#
The C# provides an Array class to deal with array-related operations. It provides methods for creating, manipulating, searching, and sorting elements of an array. This class works as the base class for all arrays in the .NET programming environment. Below are the commonly used methods of array class elements of an array. This class works as the base class for all arrays in the .NET programming environment.
Sort Array: It is used to arrange array data in ascending order.
int[] numbers = { 4, 2, 8, 1, 6 };
Array.Sort(numbers);
foreach (int item in numbers)
{
Console.Write(item); // Output will be 1 2 4 6 8 .
}
ForEach: Array.ForEach(array, action)Performs the specified action on each element of the array.
int[] numbers = { 10, 20, 30, 40, 50 };
Array.ForEach(numbers, n => Console.WriteLine(n * 2)); // Output – 20,40,60,80,100
Copping Array: You can copy elements from one array to another array
int[] source = {40,35,70,20,55};
int[] destination = new int[4];
Array.Copy(source, destination, 4);
foreach (int item in destination)
{
Console.WriteLine(item); // Output will be 40, 35, 70, 20.
}
please note destination array has 4 elements hence it has copied first 4 elements from source to destination
Equals Array: Check whether both array values and index are equal or not by using Boolean Output
int[] firstArray = { 40 , 20 , 30 , 45};
int[] secondArray = { 40, 30 , 20 ,45 };
bool areEqual = Array.Equals(firstArray, secondArray);
Console.WriteLine(areEqual); // Output will be False as index of the values are not same.
Convert Array to List:
Arrays have a fixed size, which means once you define the size of an array, you cannot change it.
Lists (List<T>) are dynamic, meaning you can add or remove elements as needed without worrying about size limitations.
For example,
Define an array
int[] numbersArray = { 10, 20, 30, 40, 50 };
// Convert the array to a list
List<int> numbersList = new List<int>(numbersArray);
// Add an element to the list
numbersList.Add(25);
// Remove an element from the list
numbersList.Remove(20);
// Print the list elements
Console.WriteLine(“List elements:”);
foreach (int number in numbersList)
{
Console.WriteLine(number);
}
Output:
List elements:
10
30
40
50
25
A simple example of using an array with an if-else statement in C#. In this example, we’ll create an array of numbers and check if each number is even or odd using an if-else statement.
For example,
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 5, 8, 3, 12 };
foreach (int num in numbers)
{
if (num % 2 == 0)
{
Console.WriteLine($”{num} is even.”);
}
else
{
Console.WriteLine($”{num} is odd.”);
}
}
}
}
Explanation:
using System;
class Program
{
static void Main()
{
int[] numbers = { 10, 5, 8, 3, 12 };
Define an array of integers with specific values
foreach (int num in numbers)
Iterate through each element in the numbers array
if (num % 2 == 0)
Check if the current number is even (remainder of division by 2 is 0)
Console.WriteLine($”{num} is even.”);
Print that the number is even using string interpolation
else
If the number is not even
Console.WriteLine($”{num} is odd.”);
Print that the number is odd using string interpolation
Task:
1. Sort an Array:
Create an integer array with the values {3, 1, 4, 1, 5, 9}. Use the Array.Sort method to sort the array and print the sorted values.
2. Double Each Element:
Create an integer array with the values {1, 2, 3, 4, 5}. Use the Array.ForEach method to double each element and print the modified array.
3. Copy Array:
Create a source integer array with the values {11, 22, 33, 44, 55}. Create a destination array of size 3. Use the Array.Copy method to copy the first 3 elements from the source to the destination and print the destination array.
4. Check Equality:
Create two integer arrays, one with the values {5, 10, 15} and the other with the values {5, 10, 15}. Use the Array.Equals method to check if the arrays are equal and print the result.
5. Even or Odd Check:
Create an integer array with the values {7, 14, 21, 28, 35}. Use a foreach loop and an if-else statement to check if each number is even or odd, and print the result.
6. Sort and Print Strings:
Create a string array with the values {“banana”, “apple”, “cherry”}. Use the Array.Sort method to sort the array alphabetically and print the sorted values.
7. Find and Print Even Numbers:
Create an integer array with the values {12, 15, 18, 21, 24}. Use a foreach loop and an if-else statement to print only the even numbers from the array.