|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace ExploreCsharpSeven |
| 6 | +{ |
| 7 | + class LocalFunctions |
| 8 | + { |
| 9 | + #region LocalFunctionFactorial |
| 10 | + public static int CalculateFactorial(int n) |
| 11 | + { |
| 12 | + return nthFactorial(n); |
| 13 | + |
| 14 | + int nthFactorial(int number) => (number < 2) ? |
| 15 | + 1 : number * nthFactorial(number - 1); |
| 16 | + } |
| 17 | + #endregion |
| 18 | + |
| 19 | + public static int TestLocalFactorial() |
| 20 | + { |
| 21 | + #region LocalFunctionFactorialTest |
| 22 | + Console.WriteLine(CalculateFactorial(6)); |
| 23 | + #endregion |
| 24 | + return 0; |
| 25 | + } |
| 26 | + |
| 27 | + #region LocalFuntionIteratorMethod |
| 28 | + static IEnumerable<char> AlphabetSubset(char start, char end) |
| 29 | + { |
| 30 | + if (start < 'a' || start > 'z') |
| 31 | + throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter"); |
| 32 | + if (end < 'a' || end > 'z') |
| 33 | + throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter"); |
| 34 | + |
| 35 | + if (end <= start) |
| 36 | + throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}"); |
| 37 | + for (var c = start; c < end; c++) |
| 38 | + yield return c; |
| 39 | + } |
| 40 | + #endregion |
| 41 | + |
| 42 | + #region LocalFunctionIteratorWithLocal |
| 43 | + static IEnumerable<char> AlphabetSubsetLocal(char start, char end) |
| 44 | + { |
| 45 | + if (start < 'a' || start > 'z') |
| 46 | + throw new ArgumentOutOfRangeException(paramName: nameof(start), message: "start must be a letter"); |
| 47 | + if (end < 'a' || end > 'z') |
| 48 | + throw new ArgumentOutOfRangeException(paramName: nameof(end), message: "end must be a letter"); |
| 49 | + |
| 50 | + if (end <= start) |
| 51 | + throw new ArgumentException($"{nameof(end)} must be greater than {nameof(start)}"); |
| 52 | + |
| 53 | + return alphabetSubsetImplementation(); |
| 54 | + |
| 55 | + IEnumerable<char> alphabetSubsetImplementation() |
| 56 | + { |
| 57 | + for (var c = start; c < end; c++) |
| 58 | + yield return c; |
| 59 | + } |
| 60 | + } |
| 61 | + #endregion |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + public static int TestSubset() |
| 66 | + { |
| 67 | + #region LocalFunctionsIteratorTest |
| 68 | + try |
| 69 | + { |
| 70 | + var resultSet1 = AlphabetSubset('d', 'r'); |
| 71 | + var resultSet2 = AlphabetSubset('f', 'a'); |
| 72 | + Console.WriteLine("iterators created"); |
| 73 | + foreach (var thing1 in resultSet1) |
| 74 | + Console.Write($"{thing1}, "); |
| 75 | + Console.WriteLine(); |
| 76 | + foreach (var thing2 in resultSet2) |
| 77 | + Console.Write($"{thing2}, "); |
| 78 | + Console.WriteLine(); |
| 79 | + } |
| 80 | + catch (ArgumentException) |
| 81 | + { |
| 82 | + Console.WriteLine("Caught an argument exception"); |
| 83 | + } |
| 84 | + return 1; |
| 85 | + #endregion |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | +} |
0 commit comments