Artem Community
Artem interactive communities
Two simple Extension Methods for Reader

I have added to my Extension Methods library a very useful ForEachLine methods, that extends the TextReader and FileInfo functionality.
The purpose of those methods is simple: provides ability to provide action to invoked for each line of a TextReader of file.

Here are the methods for anyone how find them useful, as well.

TextReader ForEachLine Extension Method

public static void ForEachLine(this TextReader reader, Action<string> action) {

 

            if (reader == null)

                throw new ArgumentNullException("reader");

            if (action == null)

                throw new ArgumentNullException("action");

            string line;

            while ((line = reader.ReadLine()) != null) {

                action(line);

            }

        }

FileInfo ForEachLine Extension Method

public static void ForEachLine(this FileInfo file, Action<string> action) {

 

            if (file == null)

                throw new ArgumentNullException("file");

            using (StreamReader reader = file.OpenText()) {

                reader.ForEachLine(action);

            }

        }

Happy coding ...


Posted 12-16-2008 14:45 by velio
Powered by Community Server (Non-Commercial Edition), by Telligent Systems