×

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as a base class for all types of arrays in .Net programming. Signature of Array class:
[SerializableAttribute]  
[ComVisibleAttribute(true)]  
public abstract class Array : ICloneable, IList, ICollection,   
IEnumerable, IStructuralComparable, IStructuralEquatable
C# Array class Properties
Property Description
IsFixedSize It is used to get a value indicating whether the Array has a fixed size or not.
IsReadOnly It is used to check that the Array is read-only or not.
IsSynchronized It is used to check that access to the Array is synchronized or not.
Length It is used to get the total number of elements in all the dimensions of the Array.
LongLength It is used to get a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
Rank It is used to get the number of dimensions of the array.
SyncRoot It is used to get an object that can be used to synchronize access to the Array.
 C# Array class Methods
Method Description
AsReadOnly<T>(T[]) It returns a read-only wrapper for the specified array.
BinarySearch(Array,Int32,Int32,Object) It is used to search a range of elements in a one-dimensional sorted array for a value.
BinarySearch(Array,Object) It is used to search an entire one-dimensional sorted array for a specific element.
Clear(Array,Int32,Int32) It is used to set a range of elements in an array to the default value.
Clone() It is used to create a shallow copy of the Array.
Copy(Array,Array,Int32) It is used to copy elements of an array into another array by specifying starting index.
CopyTo(Array,Int32) It copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index
CreateInstance(Type,Int32) It is used to create a one-dimensional Array of the specified Type and length.
Empty<T>() It is used to return an empty array.
Finalize() It is used to free resources and perform cleanup operations.
Find<T>(T[],Predicate<T>) It is used to search for an element that matches the conditions defined by the specified predicate.
IndexOf(Array,Object) It is used to search for the specified object and returns the index of its first occurrence in a one-dimensional array.
Initialize() It is used to initialize every element of the value-type Array by calling the default constructor of the value type.
Reverse(Array) It is used to reverse the sequence of the elements in the entire one-dimensional Array.
Sort(Array) It is used to sort the elements in an entire one-dimensional Array.
ToString() It is used to return a string that represents the current object. 

 C# Array class Example:
using System;  
namespace CSharpProgram  
{  
    public class Program  
    {  
        public static void Main(string[] args)  
        {  
            // Creating an array  
            int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };  
            // Creating an empty array  
            int[] arr2 = new int[6];  
            // Displaying length of array  
            Console.WriteLine("length of first array: "+arr.Length);  
            // Sorting array  
            Array.Sort(arr);  
            Console.Write("First array elements: ");  
            // Displaying sorted array  
            PrintArray(arr);  
            // Finding index of an array element  
            Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));  
            // Coping first array to empty array  
            Array.Copy(arr, arr2, arr.Length);  
            Console.Write("Second array elements: ");  
            // Displaying second array  
            PrintArray(arr2);  
            Array.Reverse(arr);  
            Console.Write("\nFirst Array elements in reverse order: ");  
            PrintArray(arr);  
        }  
        // User defined method for iterating array elements  
        static void PrintArray(int[] arr)  
        {  
            foreach (Object elem in arr)  
            {  
                Console.Write(elem+" ");  
            }  
        }  
    }  
}
Output:
length of first array: 6
First array elements: 0 5 7 8 9 25 
Index position of 25 is 5
Second array elements: 0 5 7 8 9 25 
First Array elements in reverse order: 25 9 8 7 5 0

Related Topics

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.