Array of Arrays

Viewed 32399

How do you create an array of arrays in C#? I have read about creating jagged arrays but I'm not sure if thats the best way of going about it. I was wanting to achieve something like this:

string[] myArray = {string[] myArray2, string[] myArray3}

Then I can access it like myArray.myArray2[0];

I know that code won't work but just as an example to explain what I mean.

Thanks.

6 Answers

I wanted to do the same thing to get a list of strings that an enum represented. Here is a possible solution:

     public enum Group
     {
          All = 0,
          Artists = 1,
          Builders = 2
      }

      private static readonly string[] _rolesAll = { "Brett", "Jeff", "Virgil", "Danielle" };
      private static readonly string[] _rolesArtists = { "Brett", "Danielle" };
      private static readonly string[] _rolesBuilders = { "Jeff" };

      private static readonly SortedList<Group, string[]> _sortedGroupToRoles = new SortedList<Group, string[]> { { Group.All, _rolesAll }, { Group.Artists, _rolesArtists}, { Group.Builders, _rolesBuilders } };

then to use it:

      _sortedGroupsToRoles.TryGetValue(Group.Artists, out string[] roles);
Related