diff options
| author | Jo Pearce <jo.pearce@gmail.com> | 2015-10-12 15:15:06 +0100 | 
|---|---|---|
| committer | Jo Pearce <jo.pearce@gmail.com> | 2015-10-12 15:19:37 +0100 | 
| commit | 0cea1d25d5d103eb9774cf3f3e1362a11b2e684d (patch) | |
| tree | 841368c5864dbc8ff278be22eab46f40cc3cb6cb | |
| parent | 51f7c4f6a015666794dc46ec080c80813d4ac57e (diff) | |
Added some simple yield examples
| -rw-r--r-- | csharp.html.markdown | 39 | 
1 files changed, 38 insertions, 1 deletions
| diff --git a/csharp.html.markdown b/csharp.html.markdown index 72ee3731..8babb90a 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -6,6 +6,7 @@ contributors:      - ["Melvyn Laïly", "http://x2a.yt"]      - ["Shaun McCarthy", "http://www.shaunmccarthy.com"]      - ["Wouter Van Schandevijl", "http://github.com/laoujin"] +    - ["Jo Pearce", "http://github.com/jdpearce"]  filename: LearnCSharp.cs  --- @@ -417,6 +418,42 @@ on a new line! ""Wow!"", the masses cried";                  // Item is an int                  Console.WriteLine(item.ToString());          } +         +        // YIELD +        // Usage of the "yield" keyword indicates that the method it appears in is an Iterator +        // (this means you can use it in a foreach loop) +        public static IEnumerable<int> YieldCounter(int limit = 10) +        { +            for (var i = 0; i < limit; i++) +                yield return i; +        } + +        // which you would call like this : +        public static void PrintYieldCounterToConsole() +        { +            foreach (var counter in YieldCounter()) +                Console.WriteLine(counter); +        } +         +        // you can use more than one "yield return" in a method +        public static IEnumerable<int> ManyYieldCounter() +        { +            yield return 0; +            yield return 1; +            yield return 2; +            yield return 3; +        } +         +        // you can also use "yield break" to stop the Iterator +        // this method would only return half of the values from 0 to limit. +        public static IEnumerable<int> YieldCounterWithBreak(int limit = 10) +        { +            for (var i = 0; i < limit; i++) +            { +                if (i > limit/2) yield break; +                yield return i; +            } +        }                       public static void OtherInterestingFeatures()          { @@ -875,7 +912,7 @@ on a new line! ""Wow!"", the masses cried";  ## Topics Not Covered   * Attributes - * async/await, yield, pragma directives + * async/await, pragma directives   * Web Development   	* ASP.NET MVC & WebApi (new)   	* ASP.NET Web Forms (old) | 
