TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# File.ReadAllLines, Get String Array From File

Call the File.ReadAllLines method from System.IO to get a string array from a file.
File.ReadAllLines returns an array. This array contains a string for each line in the file specified. We use this method. We then look inside its .NET Framework implementation to see how it works. It is found in the System.IO namespace.File
Example. This example program specifies a file on the disk. To have it run correctly, change the filename to one that exists. ReadAllLines gets a string array from the file. The string array returned can be used as any other string array.

Then: We can use "foreach" or "for" to loop over the file's data in a clean and intuitive way.

Tip: This program reads in a file, counts its lines, and counts all chars in each line.

C# program that calls File.ReadAllLines using System; using System.IO; class A { static void Main() { string[] lines = File.ReadAllLines("C:\\rearrange.txt"); Console.WriteLine("Length: {0}", lines.Length); Console.WriteLine("First: {0}", lines[0]); int count = 0; foreach (string line in lines) { count++; } int c = 0; for (int i = 0; i < lines.Length; i++) { c++; } Console.WriteLine("Count: {0}", count); Console.WriteLine("C: {0}", c); } } Output Length: 430 First: /_1=D1 Count: 430 C: 430
Implementation. The File.ReadAllLines method is not a low-level implementation. Instead, it uses a List and the StreamReader type, and then ToArray, to return the array. This is inefficient if you want to put a file into a List.
If you need to optimize a File.ReadAllLines call, you could estimate the capacity of the List in its constructor, which would reduce resizes. You could also avoid ToArray if you want to keep the List.ListStreamReaderToArrayCapacity
Summary. We looked at the File.ReadAllLines method in the .NET Framework version 4.0. This is a convenience method that uses StreamReader and List internally. It can be useful in quick programs where you simply want all the lines in a file.

But: If you need to save memory, reading in the lines one-by-one and processing them as you read them would be effective.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf