简体   繁体   中英

How to make an Enumerable Range List of Lists?

I can make a List of chars like this:

List<char> letter = Enumerable.Range(0, 10)
                              .Select(i => '\0')
                              .ToList();

And a List of int? like this:

List<int?> number = Enumerable.Range(0, 10)
                              .Select(i => (int?)i)
                              .ToList();

And call them by letter[1] = 'a' and number[1] = 5 .


How can I make a List (or HashSet ) of 10 List<char> 's?

Something like:

List<char> myList = Enumerable.Range(0, 10)
                              .Select(i => List<char> i)
                              .ToList();

myList[1] , myList[2] , myList[3]


I want to loop through and add items to each list.

for (int i = 0; i < 10; i++)
{
    myList[i].Add(letter[i]);
}

You can combine your two approaches as follows:

List<List<char>> myList = Enumerable.Range(0, 10)
     .Select(i => Enumerable.Range(0, 10).Select(c => '\0').ToList())
     .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM