comparison MetroWpf/MetroWpf.Framework/Extensions/TextReaderExtensions.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
comparison
equal deleted inserted replaced
14:741981715d94 15:060f02cd4591
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4
5 namespace MetroWpf
6 {
7 /// <summary>
8 /// Extension methods for the TextReader class and its sub classes (StreamReader, StringReader)
9 /// </summary>
10 public static class TextReaderExtensions
11 {
12 #region · Extensions ·
13
14 /// <summary>
15 /// The method provides an iterator through all lines of the text reader.
16 /// </summary>
17 /// <param name="reader">The text reader.</param>
18 /// <returns>The iterator</returns>
19 /// <example>
20 /// <code>
21 /// using(var reader = fileInfo.OpenText()) {
22 /// foreach(var line in reader.IterateLines()) {
23 /// // ...
24 /// }
25 /// }
26 /// </code></example>
27 /// <remarks>Contributed by OlivierJ</remarks>
28 public static IEnumerable<string> IterateLines(this TextReader reader)
29 {
30 string line = null;
31
32 while ((line = reader.ReadLine()) != null)
33 {
34 yield return line;
35 }
36 }
37
38 /// <summary>
39 /// The method executes the passed delegate /lambda expression) for all lines of the text reader.
40 /// </summary>
41 /// <param name="reader">The text reader.</param>
42 /// <param name="action">The action.</param>
43 /// <example>
44 /// <code>
45 /// using(var reader = fileInfo.OpenText()) {
46 /// reader.IterateLines(l => Console.WriteLine(l));
47 /// }
48 /// </code></example>
49 /// <remarks>Contributed by OlivierJ</remarks>
50 public static void IterateLines(this TextReader reader, Action<string> action)
51 {
52 foreach (var line in reader.IterateLines())
53 {
54 action(line);
55 }
56 }
57
58 #endregion
59 }
60 }