Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/// <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