CSharp · Console
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var theOne = FindTheOne();
Console.WriteLine("The result: {0}", (theOne?.ThisIsTheOne())?.ToString() ?? "nothing");
}
private static Value FindTheOne()
{
foreach(var i in Values())
{
if (i.ThisIsTheOne())
{
Console.WriteLine("I found it!");
return i;
}
Console.WriteLine("Not this one");
}
Console.WriteLine("Sorry, not found");
return null;
}
private static IEnumerable<Value> Values()
{
var badValue = new Value(false);
var goodValue = new Value(true);
try {
yield return badValue;
yield return goodValue;
} finally {
goodValue.Die();
}
}
private class Value
{
private bool flag;
public Value(bool flag)
{
this.flag = flag;
}
public bool ThisIsTheOne() => flag;
public void Die()
{
this.flag = false;
}
}
}