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.
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
Copying 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. If you want to compare the elements, you need to use SequenceEqual
from System.Linq
.
int[] firstArray = { 40, 20, 30, 45 };
int[] secondArray = { 40, 30, 20, 45 };
bool areEqual = firstArray.SequenceEqual(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
Course Video
Course Video English
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.
The Array class in C# is a base class that provides methods and properties for creating, managing, and manipulating arrays in a type-safe manner.
Yes, the Array class supports multidimensional arrays and provides methods like GetLength() to retrieve dimensions.
Some commonly used methods are:
- Sort() – Sorts array elements.
- Reverse() – Reverses the order of elements.
- IndexOf() – Finds the index of a specific value.
- Copy() – Copies elements to another array.
Arrays have a fixed size and are ideal for static data, while List<T> is a resizable collection that is more flexible for dynamic data.
No, arrays in C# are fixed in size. To “resize,” you must create a new array and copy the elements over. The Array.Resize() method is a helper for this.
The size of an array is limited by the amount of memory available and the maximum value of a 32-bit int, which is approximately 2 billion elements.
The Array.Sort() method sorts the elements in ascending order. You can also use a custom comparer for more complex sorting.
Jagged arrays are arrays of arrays where each inner array can have a different size. The Array class can be used with jagged arrays, but they require more manual handling compared to multidimensional arrays.
Iqra Technology Academy offers detailed video tutorials in Hindi to explain the concept of class array properties in C#. Our video lessons will guide you through examples and help you understand how to implement array properties in your classes.
At Iqra Technology Academy, we offer free online courses in Hindi on topics like class array properties in C#. These courses are designed for beginners and include hands-on examples to help you understand the practical applications of class array properties.
To define a class array property in C#, you simply declare a property of type array[] within a class. This allows you to store multiple elements of the same type in an array.
Example:
class Employee
{
public string[] Skills { get; set; }
public Employee(string[] skills)
{
Skills = skills;
}
}
In this example, the Employee class has an array property Skills that holds an array of strings.
Class array properties are useful when you need to store multiple values of the same type within a class, such as a list of student grades, employee skills, or product categories. It helps in managing related data together in a structured way.
Yes, you can define class array properties with any data type, such as integers, strings, or custom class objects. For example, an array of integers or an array of custom objects like Employee[] is also possible.
The best way to learn C# class array properties is by watching tutorials, practicing with examples, and exploring real-world applications. Iqra Technology Academy provides detailed tutorials and practical examples .
Using class array properties in C# allows you to organize and manage related data efficiently. It makes your code more structured and easier to maintain when dealing with collections of items.