The following code example demonstrates How to Use Array Class in C#.

In fact, C# provides the Array class that we can use to create arrays. The Array class offers several useful methods that we can use to manipulate arrays. Apart from creating arrays using the array declaration syntax, we have the CreateInstance() method of the Array class. We can use this method to create an array of desired types and sizes. Furthermore, the CreateInstance() method is a static method that returns an Array object.

Once, we get an instance of the Array class, we can call other methods using that array. For example, the SetValue() method assigns a value to a particular array element. Similarly, we can use other methods like Sort(), Reverse(), GetValue(), GetUpperBound(), and GetLowerBound().

using System;

namespace ArrayClassDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Creating an object of the Array class...");
            Array arr1 = Array.CreateInstance(typeof(System.Int32), 6);

            arr1.SetValue(23, 0);
            arr1.SetValue(67, 1);
            arr1.SetValue(47, 2);
            arr1.SetValue(98, 3);
            arr1.SetValue(239, 4);
            arr1.SetValue(57, 5);
            Console.WriteLine("1D Array...");
            for(int i=0;i<arr1.Length;i++)
            {
                Console.Write(arr1.GetValue(i)+" ");
            }
            //Sorting Array
            Array.Sort(arr1);
            Console.WriteLine("\nSorted 1D Array...");
            for (int i = 0; i < arr1.Length; i++)
            {
                Console.Write(arr1.GetValue(i)+" ");
            }

            //Reversing Array
            Array.Reverse(arr1);
            Console.WriteLine("\nReversed 1D Array...");
            for (int i = 0; i < arr1.Length; i++)
            {
                Console.Write(arr1.GetValue(i) + " ");
            }
            // Creating a 2D array
            Array m1 = Array.CreateInstance(typeof(System.Int32), 2, 3);

            for(int i=0;i<2;i++)
            {
                for (int j = 0; j<3;j++)
                {
                    m1.SetValue(i + j, i, j);
                }
            }
            Console.WriteLine("\n2D Array...");
            for(int i = 0; i <= m1.GetUpperBound(0); i++)
            {
                for(int j=0;j<=m1.GetUpperBound(1);j++)
                {
                    Console.Write(m1.GetValue(i, j) + " ");
                }
                Console.WriteLine();
            }
       
        }
    }
}

Output

Creating Arrays Using Array Class in C#
Creating Arrays Using Array Class in C#

Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Princites

IITM Software Development Cell