C# Array Class4 min read

There are various operations that can be done with an array such as creating, manipulating, sorting, searching, etc. This is done by the array class that provides various methods to perform mentioned operation in C#.

Note: In C#, Array is not part of the collection but considered as a collection because it is based on the IList interface.


Properties of the Array Class – C#

The following table represents some of the properties of the Array class that are mostly used.

PropertyDescription
IsFixedSizeThis property is used to get a value indicating whether the Array has a fixed size or not.
IsReadOnlyThis is used to check whether the Array is read-only or not.
LengthThis property gets (32-bit integer) that represents the total number of elements in all the dimensions of the Array.
LongLengthThis property gets (a 64-bit integer) that represents the total number of elements in all the dimensions of the Array.
RankThis property gets the rank (number of dimensions) of the Array.
SyncRootIt gets an object that can be used to synchronize access to the Array.
IsSynchronizedIt checks whether the access to the Array is synchronized.

Methods of the Array Class – C#

The following table represents some of the methods of the Array class that are mostly used.

MethodDescription
BinarySearch()This method is used to search an entire one-dimensional sorted array for a specific value. It uses binary search algorithm
Clear()It is used to set a range of elements to zero, to null, or to false, depending on the type of element.
Empty()Simply returns an empty array.
Copy()This method copies elements of an array into another array by specifying the initial index and performs type casting as required.
CopyTo()Copies all the elements of the current one-dimensional array to the specified one-dimensional array.
Clone()This method creates a shallow copy of the Array.
CreateInstance()Using this method new instance of the Array class is initialized.
Initialize()This method initializes every element of the value-type Array by calling the default constructor of the value type.
IndexOf()This method searches for the specified object and returns the index of the first occurrence in a one-dimensional array.
Find()It is used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array.
Reverse()This method reverses the sequence of the elements in the entire one-dimensional Array.
ToString()It returns a string that represents the current object.
GetUpperBound()It is used to get the upper bound of the specified dimension in the Array.
GetLowerBound()It is used to get the lower bound of the specified dimension in the Array.
Sort()It is used to sort the elements in an entire one-dimensional Array by using the IComparable implementation of each element of the Array..
AsReadOnly()This method returns a read-only wrapper for the specified array.

C# program for Array Class

No let us see an example to demonstrate some of the methods of Array Class in C# programming.

Output:


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …