From the list <string []> to String [,]

I have a problem here, and it seems like my brain has just left the building, so I need you guys to help me. I have an API method that requires a multi-dimensional array of strings. It looks like this:

string[,] specialMacroArray = new string[,] { { "#macro#", "text1" }, {"#secondmacro#", "text2"} }

The contents of the array are computed throughout my method, and therefore I cannot write it as indicated above. So I put the values ​​in a list in all my code, for example:

List<string[]> specialMacros = new List<string[]>();
specialMacros.Add(new string[] { "#macro#", text1 });
specialMacros.Add(new string[] { "#secondmacro#", "text2" });

So far so good ... but now I want to convert the list to a multidimensional array. But I can’t figure out how to do this.

specialMacroArray = specialMacros.ToArray()

I am using the .NET 3.5 Framework in C #

Thanx in advance

+3
source share
4 answers

In this case, you can simply do this:

specialMacroArray = new string[specialMacros.Count, 2];
for (int i = 0; i < specialMacros.Count; i++)
{
    specialMacroArray[i, 0] = specialMacros[i][0];
    specialMacroArray[i, 1] = specialMacros[i][1];
}
+4
source

, , . - :

string[,] array = new string[specialMacroArray.Count, 2]

for (int i=0; i<specialMacroArray.Count; ++i)
{
    array[i, 0] = specialMacroArray[i][0];
    array[i, 1] = specialMacroArray[i][1];
}
+3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string[]> specialMacros = new List<string[]>();
            specialMacros.Add(new string[] { "#macro#", "text1" }); 
            specialMacros.Add(new string[] { "#secondmacro#", "text2" });
            var op = specialMacros.ToMultiDimensionalArray();
            Console.Read();
        }
    }
    public static class ArrayHelper
    {
        public static string[,] ToMultiDimensionalArray(this List<string[]> dt)
        {
            int col = dt.FirstOrDefault().ToList().Count();

            string[,] arr = new string[dt.Count, col];
            int r = 0;
            foreach (string[] dr  in dt)
            {
                for (int c = 0; c < col; c++)
                {
                    arr[r, c] = dr[c];
                }
                r++;
            }
            return arr;
        }
    }


}

Based on some comments, I edited the function, but I believe that this user can make his own judgment and improve the code if he needs it.

 public static string[,] ToMultiDimensionalArray(this List<string[]> dt)
        {
           if (dt.Count == 0 )
               throw new ArgumentException("Input arg has no elemets");
           int col = dt[0].Count();
            string[,] arr = new string[dt.Count, col];
            int r = 0;
            foreach (string[] dr in dt)
            {
                for (int c = 0; c < col; c++)
                {
                    arr[r, c] = dr[c];
                }
                r++;
            }
            return arr;
        }
+1
source
string[][] specialMacroArray = new string[][] { new string[] { "#macro#", "text1" }, new string[] { "#secondmacro#", "text2" } };
        List<string[]> specialMacros = new List<string[]>();
        specialMacros.Add(new string[] { "#macro1#", "text1" });
        specialMacros.Add(new string[] { "#secondmacro#", "text2" });
        specialMacroArray = specialMacros.ToArray();

works fine, the loop is not needed ... all you have to do is change string[,]to string[][](this array array is initialized to bit-by-bit)

updated code:

List<string[,]> specialMacros = new List<string[,]>();
specialMacros.Add(new string[,] { { "#secondmacro#", "text2" }, { "#secondmacro#", "text2" } });
specialMacroArray =specialMacros[0];
0
source

All Articles