1

Is it possible to add multiple items into list or adding a list of values into a list.

here is my current pseudo code to do it:

List<string> myList = new List<string>();
myList.add("a","b","c","d","e","f","g");       
myList.add("h","i","j","k","l","m","n");       
myList.add("a1","a2","a3"); 

and my expected result is:

[["a","b","c","d","e","f","g"], ["h","i","j","k","l","m","n"], ["a1","a2","a3"]]

Any suggestions/comments TIA.

2
  • Are you looking to create a list of lists? Commented Apr 2, 2019 at 1:57
  • yes that's what I want because later on I will loop it and then just get selected index on each list. Commented Apr 2, 2019 at 1:58

2 Answers 2

3

What you are asking for is a List<List<string>>. There are probably better structures for storing your data but since you haven't given any context, you can do this:

var myList = new List<List<string>>();

And add items like this:

myList.Add(new List<string> { "a", "b", "c", "d", "e", "f", "g" });
myList.Add(new List<string> { "h", "i", "j", "k", "l", "m", "n" });
myList.Add(new List<string> { "a1", "a2", "a3" });

Or in one piece of code using a collection initialiser:

var myList = new List<List<string>>
{
    new List<string> { "a", "b", "c", "d", "e", "f", "g" },
    new List<string> { "h", "i", "j", "k", "l", "m", "n" },
    new List<string> { "a1", "a2", "a3" }
};
Sign up to request clarification or add additional context in comments.

6 Comments

You could also use the AddRange method to add more than one value at a time.
@Mick I'm not sure AddRange makes the code any more readable in this situation. We would need to create an IEnumerable which is unnecessary.
@DavidG, I think that Mick is talking about replacing 3x myList.Add(new List<string> { .. } with myList.AddRange(new [] { new List<string> { .. }, new List<string> { .. }, new List<string> { .. } }); which looks just like collection initializer.
@vasily.sib Yes, and I'm saying that's a horrible way to do it. That creates an intermediate array object that isn't needed, and I think makes it less readable.
@DavidG, can you pls explain what is so horrible with that? With just a little formatting this will look almost the same as collection initializer and an intermediate array object will be disposed by GC as soon as posible. Beside that, question was about adding values to the list, so AddRange example looks handy.
|
2

Should be as easy as

var myList = new List<List<string>>()
         {
            new List<string> { "a", "b", "c", "d", "e", "f", "g" },
            new List<string> { "h", "i", "j", "k", "l", "m", "n" },
            new List<string> { "a1", "a2", "a3" },
         };
// OR

var myarray = new[]
          {
             new[] { "a", "b", "c", "d", "e", "f", "g" },
             new[] { "h", "i", "j", "k", "l", "m", "n" },
             new[] { "a1", "a2", "a3" },
          };

Additional Resources

Object and Collection Initializers (C# Programming Guide)

C# lets you instantiate an object or collection and perform member assignments in a single statement.

Collection initializers

Collection initializers let you specify one or more element initializers when you initialize a collection type that implements IEnumerable and has Add with the appropriate signature as an instance method or an extension method. The element initializers can be a simple value, an expression, or an object initializer. By using a collection initializer, you do not have to specify multiple calls; the compiler adds the calls automatically.

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.