C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
But: If you need to save memory, reading in the lines one-by-one and processing them as you read them would be effective.