Read a File into List in C#
To read file into list strings in C#, we use ReadLines()
methods that returns an IEnumerable
, and then we create a new List of strings and add range using AddRange()
method.Example
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
IEnumerable<string> lines = System.IO.File.ReadLines(@"C:\File_Path.txt");
List<string> listOfLines = new List<string>();
listOfLines.AddRange(lines);
}
}