what you need is a Jugged array (not a multi-dimensional array)
https://msdn.microsoft.com/en-us/library/2s05feca.aspx
int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[5]; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 };
full example with columns:
using System; using System.Collections.Generic; namespace Rextester { public class Program { public static T[] column<T>(T[][] jaggedArray,int wanted_column) { T[] columnArray = new T[jaggedArray.Length]; T[] rowArray; for(int i=0;i<jaggedArray.Length;i++) { rowArray=jaggedArray[i]; if(wanted_column<rowArray.Length) columnArray[i]=rowArray[wanted_column]; } return columnArray; } public static void Main(string[] args) {
another same idea using extensions
using System; using System.Collections.Generic; namespace Rextester { public static class MyExtensions { public static string Extend(this Array array) { return "Yes, you can extend an array"; } public static T[] column<T>(this T[,] multidimArray,int wanted_column) { int l=multidimArray.GetLength(0); T[] columnArray = new T[l]; for(int i=0;i<l;i++) { columnArray[i]=multidimArray[i,wanted_column]; } return columnArray; } public static T[] row<T>(this T[,] multidimArray,int wanted_row) { int l=multidimArray.GetLength(1); T[] rowArray = new T[l]; for(int i=0;i<l;i++) { rowArray[i]=multidimArray[wanted_row,i]; } return rowArray; } } public class Program { public static void Main(string[] args) { Console.WriteLine("Hello, world!"); int [,] multidimArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; Console.WriteLine(string.Join(" ",multidimArray.column(0))); Console.WriteLine(string.Join(" ",multidimArray.row(0))); } } }
source share