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];
}
5
  • 4
    var list = Number.ToList() Commented Dec 12, 2018 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. Commented Dec 12, 2018 at 20:42
  • don't use magic numbers, just use Number.Length or Number.Length-1 (whichever is most appropriate) instead of i<125 Commented Dec 12, 2018 at 20:43
  • 1
    Rather than reading an array and converting to a List<string> you could create the list directly by using File.ReadLines(), e.g. var listNumbers = File.ReadLines("Numbers.txt").Take(125).ToList(); Commented Dec 12, 2018 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++) Commented Dec 12, 2018 at 21:38

3 Answers 3

5

just call ToList():

myArray.ToList();

or:

var list = new List<string>(myArray);
Sign up to request clarification or add additional context in comments.

Comments

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();

1 Comment

If you're using Linq, you could use the newer File.ReadLines() method instead, although as the OP is buffering all the lines anyway, it'll make little difference in this use case.
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

1 Comment

Oh, of course, that makes sense. Removing comment... :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.