Get Dimensions of array - CSharp System

CSharp examples for System:Array Dimension

Description

Get Dimensions of array

Demo Code


using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;//from  ww  w  . j  a  va2s .  c  o m

public class Main{
        public unsafe static int[] GetDimensions(object generalArray)
        {
            int[] dimensions = null;
            Type arrayType = generalArray.GetType();
            if (generalArray is Array) 
            {
                Array array = generalArray as Array;
                dimensions = new int[array.Rank];
                for (int i = 0; i < array.Rank; ++i) dimensions[i] = array.GetLength(i);
            }
            else if (arrayType.Name == "ndarray")
            {
                dynamic dynamicArray = generalArray;
                dimensions = new int[dynamicArray.Dims.Length];
                for (int i = 0; i < dimensions.Length; ++i) dimensions[i] = (int)dynamicArray.Dims[i];
            }
            // for jagged lists or enumerables
            else if (generalArray is IEnumerable<object>)
            {
                dimensions = DimensionsOfJaggedIEnumerable(generalArray as IEnumerable<object>);
            }
            return dimensions;
        }
}

Related Tutorials