Created
August 28, 2016 16:13
-
-
Save kemiller2002/6c9c92dcaec057f756baf4a9df9757d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// This has lazy evaluation, so you won't find out if the argument is the cause of an exception | |
/// until you start the enumeration. | |
/// </summary> | |
/// <param name="word"></param> | |
/// <returns></returns> | |
public static IEnumerable<char> MakeUpperCaseAndReturnParts (string word) | |
{ | |
if (String.IsNullOrWhiteSpace(word)) | |
{ | |
throw new ArgumentException("input is null"); | |
} | |
var uppercase = word.ToUpper(); | |
foreach(var c in uppercase) | |
{ | |
yield return c; | |
} | |
} | |
/// <summary> | |
/// Since the enumeration is encapsulated in another function, the evaluation of the paratmer is | |
/// immediate, allowing you to notice the exception when the method is called | |
/// and not evaulated at some later time. | |
/// </summary> | |
/// <param name="word"></param> | |
/// <returns></returns> | |
public static IEnumerable<char> MakeUpperCaseAndReturnPartsLocalFunction (string word) | |
{ | |
if (String.IsNullOrWhiteSpace(word)) | |
{ | |
throw new ArgumentException("input is null"); | |
} | |
IEnumerable<char> EnumerateChars () | |
{ | |
var uppercase = word.ToUpper(); | |
foreach (var c in uppercase) | |
{ | |
yield return c; | |
} | |
} | |
return EnumerateChars(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment