Skip to content

Instantly share code, notes, and snippets.

@kemiller2002
Created August 28, 2016 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemiller2002/6c9c92dcaec057f756baf4a9df9757d4 to your computer and use it in GitHub Desktop.
Save kemiller2002/6c9c92dcaec057f756baf4a9df9757d4 to your computer and use it in GitHub Desktop.
/// <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