# Clean code for array comparison

Currently it checks \r and \n for line delimiters.

        private static int GetLineCount(string fileName)
{
int lineCount = 0;

char newChar = new char();

do
{
if (lastChar == '\r' && newChar == '\n')
{
lineCount++;
}
lastChar = newChar;
return lineCount;
}


I want to use Environment.NewLine string and make it work on windows\unix.

I want to refactor above to find word occurance and then match for the word Environment.NewLine. The issue is that I am not able to refactor following for word (more specifically change lastChar , newChar into Array.

                do
{
if (lastChar == '\r' && newChar == '\n')
{
lineCount++;
}
lastChar = newChar;

-
Both BinaryReader and File.OpenRead() are IDisposable resources - wrap them in a using statement for deterministic disposal. –  Jesse C. Slicer Jan 12 '13 at 17:10
Wait, are you trying to do line count or an array comparison (as the title suggests)? Also, do you need an exact count in the case of a large file? If you know the total file size and you know the length of the first 20,000 lines, then you could have a good guess of what the line count is approximately. What are you trying to do at the higher level? –  Leonid Jan 12 '13 at 18:55
Array-comparison (or specifically word comparison) comes into picture as I want to use Environment.NewLine. At higher level I am finding exact line count for big files ( >1GB) for progress reporting. Currently It is done using Stream Current Read position divided by file length. But that is not acceptable (as inconsistent due to other file types involved), in our overall context. –  Tilak Jan 12 '13 at 19:00

Environment.NewLine always returns \r\n so it won't help you in parsing different line endings.

If your task is to count the number of lines then it would be much easier just to do smth. like:

    private static int GetLineCount(string fileName)
{
}


ReadLines method automatically parses different line endings.

-
1. Environment.NewLine returns line feed. On unix it is LF (\n), on Mac it is CR(\r), on windows it is CRLF(\r\n). 2. Try running the code on large files (> 2 GB), It will throw OutOfMemoryException. –  Tilak Jan 12 '13 at 12:57
@Tilak: Why would you line count a 2gb file?... –  acidzombie24 Jan 12 '13 at 19:49
2GB is for illustration, Files are large in size (100MB - 1GB), but to answer your question, I need to show message like Processing x row in Total N rows. –  Tilak Jan 12 '13 at 19:53
@Tilak, I've used ReadLines method that returns a stream of lines. You probably confused it with ReadAllLines which indeed loads all lines in memory. The only case when this code would throw OutOfMemoryException is when your file would have a line longer than 2GBs which is very-very unlikely for text files –  almaz Jan 12 '13 at 20:22
@Tilak about Environment.NewLine - are you talking about running this .NET code on Mac/Unix? Or parsing files coming from those platforms? .NET framework explicitly specifies \r\n as a value for Environment.NewLine, not sure about Mono. –  almaz Jan 12 '13 at 20:30