Clean code for array comparison Following snippet reads CSV Line count using BinaryReader. 

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

        private static int GetLineCount(string fileName)
        {
            BinaryReader reader = new BinaryReader(File.OpenRead(fileName));
            int lineCount = 0;

            char lastChar = reader.ReadChar();
            char newChar = new char();

            do
            {
                newChar = reader.ReadChar();
                if (lastChar == '\r' &amp;&amp; newChar == '\n')
                {
                    lineCount++;
                }
                lastChar = newChar;
            } while (reader.PeekChar() != -1);
            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
                {
                    newChar = reader.ReadChar();
                    if (lastChar == '\r' &amp;&amp; newChar == '\n')
                    {
                        lineCount++;
                    }
                    lastChar = newChar;
                } while (reader.PeekChar() != -1);
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)
    {
        return File.ReadLines(fileName).Count();
    }


ReadLines method automatically parses different line endings.