Working with Arrays in C#

This Post will look in to several Methods provided with arrays.

There are several ways to declare and initialize an array

  // ways  to declare and initialize an Array

DateTime[] dt = new DateTime[2];
DateTime[] dt2 = new DateTime[2] { new DateTime(), new DateTime() };
DateTime[] dt3 = new DateTime[] { new DateTime(), new DateTime() };
DateTime[] dt4 = { new DateTime(), new DateTime() };

we will take a character array and see what can we do with its methods

like the above sample we can declare our character array using following syntax.

char[] charArr =
    { 'c', 'o', 'd', 'i', 'g', 'p', 'h', 'o', 'b', 'i', 'a' };

Here are some of the built-in methods

           // Get the Length of array, takes 
           // dimension and returns and int
            Console.WriteLine("Length of Array is {0}", charArr.GetLength(0));

            // Get the length of array, takes 
            // dimension and returns and long
            Console.WriteLine("Length of Array is {0}", charArr.GetLongLength(0));

            // Get the dimensio  od the array, o in
            // for single dimension as for this case
            Console.WriteLine("Array has {0} dimensions", charArr.Rank);

            // Get the type of array, whic in this
            // case is System.Char[] 
            Console.WriteLine("Type of Array is : {0}", charArr.GetType());

            // Get the Lowe Bound for the Array
            Console.WriteLine("Lower bound for the array = {0}", charArr.GetLowerBound(0));

            // Get the upper bound for the Array
            Console.WriteLine("Upper bound for the array = {0}", charArr.GetUpperBound(0));

            // Get  the calue at value '0'
            Console.WriteLine("Value at 0 location is {0} ", charArr.GetValue(0));

            // initialize Array
            Console.WriteLine("Initializing Array"); charArr.Initialize();

            // check is the Array is of Fixed size
            Console.WriteLine("is Array of fixed size: {0}", charArr.IsFixedSize);

            // check is the array is read only
            Console.WriteLine("is Array read only: {0}", charArr.IsReadOnly);

            // Get a new string from the Array
            // using constructor. copy from index 0 
            // next 5 items
            string value = new string(charArr, 0, 5);

            // Get  the string representation of Array
            value = charArr.ToString();
            
            // Set value to 'j' at location '0'
            charArr.SetValue('j', 0);

            // Copy array to another Aray. size of the
            // destination Array must >= from Array
            charArr.CopyTo(new char[30], 0);

            // Compare Array to another Object
            charArr.Equals(" ");

            // Get hashcode for the Array
            Console.WriteLine("Hash Code of Array is {0}", charArr.GetHashCode());