Search code examples
c#arraysdynamic-arrays

How to create n number of Arrays Dynamically in c#?


First I will ask users "How many names you want to enter?". Once they have entered,I wanted to create that number of arrays. How to create n number of Arrays Dynamically in c#?

Console.WriteLine("How many names you want to Enter?");
int nameCount = Convert.ToInt32(console.Readline());

//I know following code is not possible.But How to make it possible?

for(int i=1;i<=nameCount;i++)
{
  string[] name+i = new string[45];
//what I mean is string[] name1 = new string[45];
  string[] name+i = new string[45];
  string[] name+i = new string[45];
  string[] name+i = new string[45];
  .
  .
  .
}

Solution

  • Try List<string[]>

    List<string[]> list=new List<string[]>();
    for(int i=1;i<5;i++)
     {
       list.add(new string[45]); 
      }
    

    Or

     Dictionary<string,string[]> dictionary;
     dictionary=new Dictionary<string,string[]>();
     for(int i=1;i<=5;i++)
      {
       dictionary.add("string" + i,new string[45]);
      }