0

Is there a standard method or property to obtain all the values of a multidimensional array in form of a vector in C#?

int[,] array = new int[2, 2] { {1, 2}, {1, 2} };
int[] vector = array.AllValues(); // ??
vgorosh
  • 25
  • 6
  • 1
    Maybe a duplicate of : https://stackoverflow.com/questions/641499/convert-2-dimensional-array – Fabske Jul 05 '18 at 16:42
  • @Fabske, you probably right and the projection provided can be considered as a standard approach... – vgorosh Jul 05 '18 at 16:54
  • find my tested answer @vgorosh – Hitesh Anshani Jul 05 '18 at 22:14
  • Can you say what you are doing here? There is probably a better way to do what you want. – Eric Lippert Jul 05 '18 at 22:41
  • @EricLippert: I'm trying to extract a one-dimensional array from a rectangular array. As Marc Gravell suggested [link](https://stackoverflow.com/questions/641499/convert-2-dimensional-array) the standard way to do it is to use the following projection: `int[] to = from.Cast().ToArray();` – vgorosh Jul 07 '18 at 04:55

1 Answers1

0

Check This one:-

int[,] array = new int[,] {{1,2},{3,4},{5,6}};
int[] vector = array.Cast<int>().ToArray(); 

Tested:-

class Program
    {
        static void Main(string[] args)
        {
            int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
            int[] vector = array.Cast<int>().ToArray();
            Console.ReadKey();
        }
    }
Hitesh Anshani
  • 1,499
  • 9
  • 19