Reputation: 2388
I've looked around but can't find my answer.
I have this code:
List<String[]> _myList = new List<String[]>();
I just want to initialize it with values but I don't know how. Thanks a lot
Upvotes: 0
Views: 828
Reputation: 1
First off, a List is not an array in that a list can have a variable number of items in it; on the other hand, an array has a fixed number of items or members which must be declared and adhered to.
using System.Collections.Generic;
Class Program
{
Static Void Main(string[] args)
Var names = new List<string>
{
"Dave",
"Steve",
"Joe",
};
// you can get additional names added by using the Add keyword
names.Add("Hillary")
// If you want to print your list you can use the foreach loop
foreach(string name in names)
Console.Writeline(names);
}
Upvotes: 0
Reputation: 5357
List<string[]> _myList = new List<string[]>() { new string[1] { "Cool" }, new string[2] { "Also", "cool" } };
Upvotes: 0
Reputation: 432
You can do it like this:
List<String[]> _myList = new List<String[]>()
{
new string[] { "string", "more string" },
new string[] { "and more string"},
};
or add after initialization like this:
_myList.Add(new string[] {"add more string"});
Upvotes: 2
Reputation: 53958
You could try something like this:
List<String[]> _myList = new List<String[]> { new String[] { "a", "b", "c", "d"},
new String[] { "a", "b"},
new String[] { "b", "c"} };
This is the collection initializer syntax. For more information about this, please have a look here.
Upvotes: 2
Reputation: 21757
Try this:
List<String[]> _myList = new List<String[]>{new String[]{"a","b"},new String[]{"c","d"}};
This is the List Initializer
syntax.
Upvotes: 3