0

I have been working on a small project and I have ran into this issue. I have a txt file full of lines and I need to store them in a List. Is there any elegant way of doing it? This is my code, however, it won´t work because something is out of bonds. The txt file have 126 lines but I need only 125 of them. Thank you for your time, any help is appreciated :)

string[] Number = System.IO.File.ReadAllLines("Numbers.txt");
List<string> listNumbers = new List<string>(); //place where all numbers will be stored
for (int i = 0; i<125; i++)
{
    listNumbers[i] = Number[i];
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
J.Doe
  • 11
  • 1
  • 4
    `var list = Number.ToList()` – JSteward Dec 12 '18 at 20:38
  • "The txt file have 126 lines but I need only 125 of them". You do need all 126 of them. From 0 to 125, there is 126 elements, not 125, because you are counting from 0. – Xiaoy312 Dec 12 '18 at 20:42
  • don't use `magic` numbers, just use `Number.Length` or `Number.Length-1` (whichever is most appropriate) instead of `i<125` – Ousmane D. Dec 12 '18 at 20:43
  • 1
    Rather than reading an array and converting to a `List` you could create the list directly by using [`File.ReadLines()`](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlines?view=netframework-4.7.2), e.g. `var listNumbers = File.ReadLines("Numbers.txt").Take(125).ToList();` – dbc Dec 12 '18 at 21:15
  • The only thing technically wrong with your code is that you've hard-coded the upper bounds of the array in your `for` loop. If you're going to loop through an array, use it's `Length` property as the upper bounds to avoid an `IndexOutOfRangeException`: `for (int i = 0; i < Number.Length; i++)`. And if you really do want to restrict the number of items to a maximum of 125, you can just add that to the condition: `for (int i = 0; i < Math.Min(Number.Length, 125); i++)` – Rufus L Dec 12 '18 at 21:38

3 Answers3

5

just call ToList():

myArray.ToList();

or:

var list = new List<string>(myArray);
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
4

An Array<string> implements IEnumerable<string>, so if you use System.Linq, a bunch of convenient extension methods are available.

using System.Linq;

// ...

var listNumbers = System.IO.File
    .ReadAllLines("Numbers.txt")
    .Take(125)
    .ToList();
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • 2
    If you're using Linq, you could use the newer [File.ReadLines()](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlines?view=netframework-4.7.2) method instead, although as the OP is buffering all the lines anyway, it'll make little difference in this use case. – Trevor Dec 12 '18 at 20:47
0

List has an AddRange() method that takes an enumerable (such as your array) and adds all the items in it, to the list. It's useful because it doesn't require LINQ, and unlike passing the array into the List constructor, it can be used if the list is constructed elsewhere/already instantiated by some other process

    //if your list is constructed elsewhere 
     List<string> listNumbers = new List<string>(); 

    //addrange can still be used to populate it 
    string[] lines = System.IO.File.ReadAllLines("Numbers.txt");
    listNumbers.AddRange(lines);

A similar InsertRange can be used to put all the values from an enumerable, into the list at a particular position

If you have a requirement to only put a certain number of items into the list, the most compact method is probably to use linq:

var list = lines.Take(125).ToList();

The next most compact is to do as you have done, with a for loop

Caius Jard
  • 72,509
  • 5
  • 49
  • 80