1

I need to create an array of string arrays in C#. Basically, I want to do the following, kind of a hybrid C/C#:

private string[] revA = new string[] {"one", "two", "three" };
private string[] revB = new string[] {"four", "five", "six" };
private string[] revC = new string[] {"seven", "eight", "nine" };

string[,] list = {revA, revB, revC};

string outStr = list[0][0];  // outStr == "one"
string outStr = list[2][1];  // outStr == "eight"

But I know that won't work.

I've looked into ArrayList and List, but I'm not sure how to make it work. Any help would be appreciated.

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
Digilee
  • 35
  • 6
  • 2
    `string[,]` is different than `string[][]`, did you mean to use the latter? – Joe Sewell Jun 24 '21 at 02:25
  • 2
    This is an easy problem, you best course of action it to work out the difference between an multidimensional array and jagged array and you will have your solution. You can start here https://www.google.com/search?q=multidimensional+vs+jagged+array+C%23&rlz=1C1CHBF_en-GBAU822AU822&oq=multidimensional+vs+jagged+array+C%23&aqs=chrome..69i57j0i22i30.7120j0j7&sourceid=chrome&ie=UTF-8 – TheGeneral Jun 24 '21 at 02:28
  • Could also use List, I find lists easier to deal with in C#. To define it use: private var revA = new List(); then you can add them using: revA.Add('item'); – Vuk Jun 24 '21 at 02:33
  • ArrayList is very old. Prefer List in all cases other than dealing with legacy code that cannot be upgraded. Upgrade legacy code that can – Caius Jard Jun 24 '21 at 05:45

4 Answers4

3

You need to use Jagged Array string[][] instead of Multidimensional Array string[,]

This source can give you more detail about it Why we have both jagged array and multidimensional array?

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
tontonsevilla
  • 2,649
  • 1
  • 11
  • 18
  • I had actually tried the string[][] syntax earlier, but the compiler did not let me initialize the list variable in the global space. "A field initializer cannot reference the non-static field, method or property 'revA'" When I moved 'list' inside a function, it worked. – Digilee Jun 24 '21 at 17:17
1
string[,] list = new string[3, 3] {{"one", "two", "three" },
                                  {"four", "five", "six" },
                                  {"seven", "eight", "nine" } };



string outStr = list[0, 0];  // outStr == "one"
Thomson Mixab
  • 657
  • 4
  • 8
  • Though I would avoid doing this; multidim arrays are generally more of a pain in the ass to work with than jaggeds – Caius Jard Jun 24 '21 at 05:48
0

This can be possible with ArrayList and with List in C#. I tried the following code you can check and explore more according to your requirements.

string[] reva = new string[] { "one", "two", "three" };
        string[] revb = new string[] { "four", "five", "six" };
        string[] revc = new string[] { "seven", "eight", "nine" };


        ArrayList arrayList = new ArrayList() { reva, revb, revc };

        List<string> nums = reva.Cast<string>().ToList();
        nums.ForEach(Console.WriteLine);
        Console.ReadLine();




        Console.ReadLine();
0

While jagged and multidimensional arrays have been mentioned, a third option is a custom multidimensional array type. Something like

    public class My2DArray<T>
    {
        public T[] Array { get; }
        public int Width { get; }
        public int Height { get; }

        public My2DArray(int width, int height, params T[] initialItems)
        {
            Width = width;
            Height = height;
            Array = new T[width * height];
            initialItems.CopyTo(Array, 0);
        }
        public T this[int x, int y]
        {
            get => Array[y * Width + x];
            set => Array[y * Width + x] = value;
        }
    }

This has the advantage over multidimensional array in that the backing single dimensional array is directly accessible, and this can be good for interoperability, or if you just want to iterate over all items.

You can add constructors to initialize it however you want.

JonasH
  • 28,608
  • 2
  • 10
  • 23