From 814e870a8807399fd6b2b877c2445e273ede260c Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sat, 21 Dec 2013 02:20:33 +0100 Subject: [PATCH] C# In Depth: Chapter 6 --- CSharpInDepth/Chapter6/Chapter6.userprefs | 2 +- CSharpInDepth/Chapter6/Chapter6/Main.cs | 146 ++++++++++++++++++--- .../Chapter6/bin/Debug/TheLayofLeithian.txt | 5 + 3 files changed, 135 insertions(+), 18 deletions(-) create mode 100644 CSharpInDepth/Chapter6/Chapter6/bin/Debug/TheLayofLeithian.txt diff --git a/CSharpInDepth/Chapter6/Chapter6.userprefs b/CSharpInDepth/Chapter6/Chapter6.userprefs index 2147a1e..4e1c5cc 100644 --- a/CSharpInDepth/Chapter6/Chapter6.userprefs +++ b/CSharpInDepth/Chapter6/Chapter6.userprefs @@ -2,7 +2,7 @@ - + diff --git a/CSharpInDepth/Chapter6/Chapter6/Main.cs b/CSharpInDepth/Chapter6/Chapter6/Main.cs index bbd61f5..7212898 100644 --- a/CSharpInDepth/Chapter6/Chapter6/Main.cs +++ b/CSharpInDepth/Chapter6/Chapter6/Main.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Threading; +using System.IO; namespace Chapter6 { @@ -16,14 +18,13 @@ namespace Chapter6 * Listings 6.1 and 6.2 Skeleton of th new collection type, with no iterator implementation. */ object[] values = {"a", "b", "c", "d", "e"}; - IterationSampleBad badCollection = new IterationSampleBad(values, 3); + IterationSampleBad badCollection = new IterationSampleBad (values, 3); try { - foreach(object x in badCollection) - { - Console.WriteLine(x); + foreach (object x in badCollection) { + Console.WriteLine (x); } - } catch(NotImplementedException e) { - Console.WriteLine("Listings 6.1 and 6.2 exception: {0}", e); + } catch (NotImplementedException e) { + Console.WriteLine ("Listings 6.1 and 6.2 exception: {0}", e); } @@ -31,10 +32,9 @@ namespace Chapter6 * * Listings 6.3 Nested class implementing the collection's iterator. */ - IterationSample collection = new IterationSample(values, 3); - foreach(object x in collection) - { - Console.WriteLine(x); + IterationSample collection = new IterationSample (values, 3); + foreach (object x in collection) { + Console.WriteLine (x); } @@ -42,10 +42,9 @@ namespace Chapter6 * * Listings 6.4 Iterating through the sample collection with C# 2 and yield return. */ - IterationSampleYield yieldCollection = new IterationSampleYield(values, 3); - foreach(object x in yieldCollection) - { - Console.WriteLine(x); + IterationSampleYield yieldCollection = new IterationSampleYield (values, 3); + foreach (object x in yieldCollection) { + Console.WriteLine (x); } @@ -53,9 +52,68 @@ namespace Chapter6 * * Listings 6.5 Showing the sequence of calls between an iterator and its caller. */ - IEnumerable iterable = CreateEnumerable(); - IEnumerator iterator = iterable.GetEnumerator(); - Console.WriteLine("Starting to iterate"); + IEnumerable iterable = CreateEnumerable (); + IEnumerator iterator = iterable.GetEnumerator (); + Console.WriteLine ("Starting to iterate"); + while (true) { + Console.WriteLine ("Calling MoveNext()..."); + bool result = iterator.MoveNext (); + Console.WriteLine ("... MoveNext result={0}", result); + if (!result) { + break; + } + Console.WriteLine ("Fetching Current..."); + Console.WriteLine ("... Current result={0}", iterator.Current); + } + + IEnumerable stringIterable = CreateStringEnumerable (); + IEnumerator stringIterator = stringIterable.GetEnumerator (); + Console.WriteLine ("Starting to iterate"); + while (true) { + Console.WriteLine ("Calling MoveNext()..."); + bool result = stringIterator.MoveNext (); + Console.WriteLine ("... MoveNext result={0}", result); + if (!result) { + break; + } + Console.WriteLine ("Fetching Current..."); + Console.WriteLine ("... Current result={0}", stringIterator.Current); + } + + + /** + * + * Listings 6.6 Demonstration of yield break. + */ + DateTime stop = DateTime.Now.AddSeconds (2); + foreach (int i in CountWithTimeLimit(stop)) { + Console.WriteLine ("Received {0}", i); + Thread.Sleep (300); + } + + + /** + * + * Listings 6.8 Looping over the lines in a file using an iterator block. + */ + foreach (string line in ReadLines ("TheLayofLeithian.txt")) { + Console.WriteLine(line); + } + + + /** + * + * Listings 6.7 Demonstration of yield break working with try/finally. + */ + DateTime stopFinally = DateTime.Now.AddSeconds (2); + foreach (int i in CountWithTimeLimitFinally(stopFinally)) { + Console.WriteLine ("Received {0}", i); + if (i > 3) { + Console.WriteLine ("Returning"); + return; + } + Thread.Sleep (300); + } } static IEnumerable CreateEnumerable () @@ -73,6 +131,60 @@ namespace Chapter6 Console.WriteLine("{0}End of CreateEnumerable()", Padding); } + + static IEnumerable CreateStringEnumerable () + { + string[] values = {"a", "b", "c"}; + Console.WriteLine ("{0}Start of CreateStringEnumerable()", Padding); + + for (int i=0; i < 3; i++) { + Console.WriteLine("{0}About to yield {1}", Padding, values[i]); + yield return values[i]; + Console.WriteLine("{0}After yield", Padding); + } + + Console.WriteLine("{0}Yielding final value", Padding); + yield return null; + + Console.WriteLine("{0}End of CreateStringEnumerable()", Padding); + } + + static IEnumerable CountWithTimeLimit (DateTime limit) + { + for (int i = 1; i <= 100; i++) { + if (DateTime.Now >= limit) + { + yield break; + } + yield return i; + } + } + + static IEnumerable CountWithTimeLimitFinally (DateTime limit) + { + try { + for (int i = 1; i <= 100; i++) { + if (DateTime.Now >= limit) { + yield break; + } + yield return i; + } + } finally { + Console.WriteLine("Stopping!"); + } + } + + static IEnumerable ReadLines (string filename) + { + using (TextReader reader = File.OpenText(filename)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + yield return line; + } + } + } } diff --git a/CSharpInDepth/Chapter6/Chapter6/bin/Debug/TheLayofLeithian.txt b/CSharpInDepth/Chapter6/Chapter6/bin/Debug/TheLayofLeithian.txt new file mode 100644 index 0000000..3066d93 --- /dev/null +++ b/CSharpInDepth/Chapter6/Chapter6/bin/Debug/TheLayofLeithian.txt @@ -0,0 +1,5 @@ +Behold! the hope of Elvenland +the fire of Fëanor, Light of Morn +before the sun and moon were born, +thus out of bondage came at last, +from iron to mortal hand it passed. -- 2.1.4