Checks whether the object is a type of Array of Arrays - CSharp System

CSharp examples for System:Array Element

Description

Checks whether the object is a type of Array of Arrays

Demo Code

/*//from w  w w. ja  va  2s .  c  o m
    * Copyright: Thomas McGlynn 1997-2007.
    * 
    * The CSharpFITS package is a C# port of Tom McGlynn's
    * nom.tam.fits Java package, initially ported by  Samuel Carliles
    *
    * Copyright: 2007 Virtual Observatory - India.     
    *
    * Use is subject to license terms
    */
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Checks whether the object is a type of Array of Arrays
        /// </summary>
        /// <param name="o"></param>
        /// <returns>Returns boolean depending upon the Arra type</returns>
        public static bool IsArrayOfArrays(Object o)
        {
            if (o == null || !o.GetType().IsArray)
            {
                return false;
            }

            // If the object passed is of type Multi-dimension Array
            if (((Array)o).Rank > 1)
            {
                IEnumerator e = ((Array)o).GetEnumerator();

                int i = 0;

                // If the argument passed is a multi-dimension array,
                // like int[2,2], below loop will give you back 'false';
                // If the input is Array[2,2]. below loop would return 'true'
                // as each element is an array.
                for (; e.MoveNext(); i++)
                {
                    if (e.Current != null && e.Current.GetType().IsArray)
                    {
                        return true;
                    }
                    else if (e.Current != null && !e.Current.GetType().IsArray)
                    {
                        return false;
                    }
                    else if (e.Current == null)
                    {
                        continue;
                    }
                }
                if (i == ((Array)o).Length)
                {
                    return false;
                }
            }
            // If the object passed is of type Jagged Array
            else
            {
                int i = 0;
                for (; i < ((Array)o).Length; i++)
                {
                    if (((Array)o).GetValue(i) != null && ((Array)o).GetValue(i).GetType().IsArray)
                    {
                        return true;
                    }
                    else if (((Array)o).GetValue(i) != null && !(((Array)o).GetValue(i).GetType().IsArray))
                    {
                        return false;
                    }
                    else if (((Array)o).GetValue(i) == null)
                    {
                        continue;
                    }
                }
                if (i == ((Array)o).Length)
                {
                    return false;
                }
            }

            return false;
            // return !(!o.GetType().IsArray && ((Array)o).Rank > 1);
        }
}

Related Tutorials