 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a 2D array into 1D array in C#?
Set a two-dimensional array and a one-dimensional array −
int[,] a = new int[2, 2] {{1,2}, {3,4} }; int[] b = new int[4]; To convert 2D to 1D array, set the two dimensional into one-dimensional we declared before −
for (i = 0; i < 2; i++) {    for (j = 0; j < 2; j++) {       b[k++] = a[i, j];    } } The following is the complete code to convert a two-dimensional array to one-dimensional array in C# −
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Program {    class twodmatrix {       static void Main(string[] args) {       int[, ] a = new int[2, 2] {          {             1,             2          },{             3,             4             }          };          int i, j;          int[] b = new int[4];          int k = 0;          Console.WriteLine("Two-Dimensional Array...");          for (i = 0; i < 2; i++) {             for (j = 0; j < 2; j++) {                Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i, j]);             }          }          Console.WriteLine("One-Dimensional Array...");          for (i = 0; i < 2; i++) {             for (j = 0; j < 2; j++) {                b[k++] = a[i, j];             }          }            for (i = 0; i < 2 * 2; i++) {             Console.WriteLine("{0}\t", b[i]);          }          Console.ReadKey();       }    } }Advertisements
 