C# Array Methods
C# provides built-in methods and properties to manipulate arrays efficiently. Commonly used methods include Length
, Reverse()
, Sort()
, and more.
Key Topics
1. Array Length Property
Example: Getting the Length of an Array
int[] numbers = { 5, 3, 8, 1, 2 };
Console.WriteLine("Length: " + numbers.Length); // Outputs "Length: 5"
2. Reversing an Array
Example: Using Array.Reverse()
Array.Reverse(numbers);
Console.WriteLine("Reversed Array:");
foreach (int num in numbers)
{
Console.WriteLine(num);
}
3. Sorting an Array
Example: Using Array.Sort()
Array.Sort(numbers);
Console.WriteLine("Sorted Array:");
foreach (int num in numbers)
{
Console.WriteLine(num);
}
Key Takeaways
Length
property gives the number of elements in the array.Array.Reverse()
reverses the elements of the array.Array.Sort()
sorts the array elements in ascending order.- Utilizing array methods simplifies data manipulation tasks.