×

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# 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# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes 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# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

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.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute 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# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 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# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 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# 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# 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# 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# 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...

5 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.