c-sharp
  1. c-sharp-array-class

C# Array Class

Syntax

public class Array : ICollection, IEnumerable, IList, IStructuralComparable, IStructuralEquatable

Example

int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Array.Sort(numbers);
foreach (int n in numbers)
{
    Console.WriteLine(n);
}

Output:

1
2
3
4
5

Explanation

The C# Array class is a built-in class that provides methods to access and manipulate arrays. An array is a collection of elements of the same data type that are stored in contiguous memory locations. The Array class in C# provides many useful methods to create, manipulate, and sort arrays.

One of the most commonly used methods of the Array class is the Sort() method. This method sorts the elements of an array in ascending order. In the above example, we create an array of integers and use the Sort() method to sort the array in ascending order. We then use a foreach loop to iterate over the sorted array and print the elements to the console.

Use

The Array class in C# provides many useful methods to work with arrays, such as creating, initializing, sorting, and resizing arrays. Some of the commonly used methods of the Array class are:

  • Sort(): Sorts the elements of an array in ascending order.
  • BinarySearch(): Searches for an element in a sorted array using a binary search algorithm.
  • Copy(): Copies elements from one array to another.
  • Resize(): Changes the size of an array.

Important Points

  • The Array class in C# provides methods to access and manipulate arrays.
  • An array is a collection of elements of the same data type that are stored in contiguous memory locations.
  • The Sort() method of the Array class sorts the elements of an array in ascending order.
  • The BinarySearch() method of the Array class searches for an element in a sorted array using a binary search algorithm.
  • The Copy() method of the Array class copies elements from one array to another.
  • The Resize() method of the Array class changes the size of an array.

Summary

The Array class in C# provides methods to create, initialize, sort, and resize arrays. It is a built-in class that provides many useful methods to work with arrays. The Sort() method of the Array class sorts the elements of an array in ascending order. The BinarySearch() method of the Array class searches for an element in a sorted array using a binary search algorithm. The Copy() method of the Array class copies elements from one array to another. The Resize() method of the Array class changes the size of an array.

Published on: