Processing a Text File with the C# yield-return Pattern

A common programming task in my environment is processing text files which have a non-flat structure using C#. I’ve found that in certain situations using the yield-return pattern is very effective here. There are several ways to think about yield-return but in my mind I think that the mechanism is way to buffer an input-to-output flow of information. In most text file processing you can just iterate through the file one line at a time. But consider the situation where after you read a line of the file, you must read more lines to complete a processing step. Here’s an example. Suppose you have a file named DataFile.txt that looks something like:


The <L> tag means a whole line. But some data is broken into two parts, the <A> and the <B> part. Now it’d be perfectly possible to process such a file using normal means but the yield-return pattern is very nice here. For example:

static void Main(string[] args)
  foreach (string line in GetDataLines(“C:\\DataFile.txt”))

static IEnumerable<string> GetDataLines(string dataFile)
  StreamReader sr = new StreamReader(dataFile);
  string line = “”;
  while ((line = sr.ReadLine()) != null) {
    if (line.StartsWith(“<L>”))
      yield return line;
    else if (line.StartsWith(“<A>”)) {
      string s = “<L>”;
      // parse out A data and add to s
      line = sr.ReadLine(); // get the <B> line
      // parse out B data and add to s
      s += “</L>”;
      yield return s;
} // GetDataLines()

The GetDataLines method uses yield-return which does a lot of buffering work for you behind the scenes, keeping track of where in the file the invisible iterator is and makes the callng code in Main very clean.

This entry was posted in Software Test Automation. Bookmark the permalink.