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.

4
  • 2
    string[,] is different than string[][], did you mean to use the latter? Commented Jun 24, 2021 at 2: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 google.com/… Commented Jun 24, 2021 at 2:28
  • Could also use List<string>, I find lists easier to deal with in C#. To define it use: private var revA = new List<string>(); then you can add them using: revA.Add('item'); Commented Jun 24, 2021 at 2:33
  • ArrayList is very old. Prefer List<T> in all cases other than dealing with legacy code that cannot be upgraded. Upgrade legacy code that can Commented Jun 24, 2021 at 5:45

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1
string[,] list = new string[3, 3] {{"one", "two", "three" },
                                  {"four", "five", "six" },
                                  {"seven", "eight", "nine" } };



string outStr = list[0, 0];  // outStr == "one"

1 Comment

Though I would avoid doing this; multidim arrays are generally more of a pain in the ass to work with than jaggeds
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();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.