11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
5- using System . Threading . Tasks ;
63
74namespace new_in_7
85{
96 public static class Iterator
107 {
11- #region 25_IteratorMethod
12- public static IEnumerable < char > AlphabetSubset ( char start , char end )
8+ public static void IteratorTest ( )
139 {
14- if ( start < 'a' || start > 'z' )
15- throw new ArgumentOutOfRangeException ( paramName : nameof ( start ) , message : "start must be a letter" ) ;
16- if ( end < 'a' || end > 'z' )
17- throw new ArgumentOutOfRangeException ( paramName : nameof ( end ) , message : "end must be a letter" ) ;
10+ // <SnippetIteratorMethod>
11+ IEnumerable < char > AlphabetSubset ( char start , char end )
12+ {
13+ if ( start < 'a' || start > 'z' )
14+ throw new ArgumentOutOfRangeException ( paramName : nameof ( start ) , message : "start must be a letter" ) ;
15+ if ( end < 'a' || end > 'z' )
16+ throw new ArgumentOutOfRangeException ( paramName : nameof ( end ) , message : "end must be a letter" ) ;
1817
19- if ( end <= start )
20- throw new ArgumentException ( $ "{ nameof ( end ) } must be greater than { nameof ( start ) } ") ;
21- for ( var c = start ; c < end ; c ++ )
22- yield return c ;
18+ if ( end <= start )
19+ throw new ArgumentException ( $ "{ nameof ( end ) } must be greater than { nameof ( start ) } ") ;
20+ for ( var c = start ; c < end ; c ++ )
21+ yield return c ;
22+ }
23+
24+
25+ try
26+ {
27+ var resultSet1 = AlphabetSubset ( 'd' , 'r' ) ;
28+ var resultSet2 = AlphabetSubset ( 'f' , 'a' ) ;
29+ Console . WriteLine ( "iterators created" ) ;
30+ foreach ( var thing1 in resultSet1 )
31+ Console . Write ( $ "{ thing1 } , ") ;
32+ Console . WriteLine ( ) ;
33+ foreach ( var thing2 in resultSet2 )
34+ Console . Write ( $ "{ thing2 } , ") ;
35+ Console . WriteLine ( ) ;
36+ }
37+ catch ( ArgumentException )
38+ {
39+ Console . WriteLine ( "Caught an argument exception" ) ;
40+ }
41+ // </SnippetIteratorMethod>
2342 }
24- #endregion
2543
26- #region 27_IteratorMethodRefactored
27- public static IEnumerable < char > AlphabetSubset2 ( char start , char end )
44+ public static void IteratorTestLocal ( )
2845 {
29- if ( start < 'a' || start > 'z' )
30- throw new ArgumentOutOfRangeException ( paramName : nameof ( start ) , message : "start must be a letter" ) ;
31- if ( end < 'a' || end > 'z' )
32- throw new ArgumentOutOfRangeException ( paramName : nameof ( end ) , message : "end must be a letter" ) ;
46+ // <SnippetIteratorMethodLocalInteractive>
47+ IEnumerable < char > AlphabetSubset ( char start , char end )
48+ {
49+ if ( start < 'a' || start > 'z' )
50+ throw new ArgumentOutOfRangeException ( paramName : nameof ( start ) , message : "start must be a letter" ) ;
51+ if ( end < 'a' || end > 'z' )
52+ throw new ArgumentOutOfRangeException ( paramName : nameof ( end ) , message : "end must be a letter" ) ;
3353
34- if ( end <= start )
35- throw new ArgumentException ( $ "{ nameof ( end ) } must be greater than { nameof ( start ) } ") ;
36- return alphabetSubsetImplementation ( start , end ) ;
37- }
54+ if ( end <= start )
55+ throw new ArgumentException ( $ "{ nameof ( end ) } must be greater than { nameof ( start ) } ") ;
3856
39- private static IEnumerable < char > alphabetSubsetImplementation ( char start , char end )
40- {
41- for ( var c = start ; c < end ; c ++ )
42- yield return c ;
57+ return alphabetSubsetImplementation ( ) ;
58+
59+ IEnumerable < char > alphabetSubsetImplementation ( )
60+ {
61+ for ( var c = start ; c < end ; c ++ )
62+ yield return c ;
63+ }
64+ }
65+
66+ try
67+ {
68+ var resultSet1 = AlphabetSubset ( 'd' , 'r' ) ;
69+ var resultSet2 = AlphabetSubset ( 'f' , 'a' ) ;
70+ Console . WriteLine ( "iterators created" ) ;
71+ foreach ( var thing1 in resultSet1 )
72+ Console . Write ( $ "{ thing1 } , ") ;
73+ Console . WriteLine ( ) ;
74+ foreach ( var thing2 in resultSet2 )
75+ Console . Write ( $ "{ thing2 } , ") ;
76+ Console . WriteLine ( ) ;
77+ }
78+ catch ( ArgumentException )
79+ {
80+ Console . WriteLine ( "Caught an argument exception" ) ;
81+ }
82+ // </SnippetIteratorMethodLocalInteractive>
4383 }
44- #endregion
4584
46- #region 28_IteratorMethodLocal
85+ // <SnippetIteratorMethodLocal>
4786 public static IEnumerable < char > AlphabetSubset3 ( char start , char end )
4887 {
4988 if ( start < 'a' || start > 'z' )
@@ -62,7 +101,7 @@ IEnumerable<char> alphabetSubsetImplementation()
62101 yield return c ;
63102 }
64103 }
65- #endregion
104+ // </SnippetIteratorMethodLocal>
66105 }
67106
68107}
0 commit comments