0

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];
  .
  .
  .
}
4
  • What about an array of arrays? Would that suit your needs? Commented Feb 3, 2012 at 3:48
  • 3
    Dynamically? Use a List<T>. Too much trouble with an array. Commented Feb 3, 2012 at 3:49
  • I'm not really sure what you are trying to achieve here, which is probably why you are getting the downvotes. Is this a homework question? Because it reads like it and that could be another reason. There is a tag for homework questions if it is one. Is your problem that you need a single array of users that expands as you add them or something else? Commented Feb 3, 2012 at 3:56
  • I think OP is just looking for a List<string>, each string instance already can store any number of characters, no other array needed. Commented Feb 3, 2012 at 4:02

1 Answer 1

5

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]);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Note that List is recreated each time number of items is over some value, so use this constructor, is you can.

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.