is there a way in .NET (or some sort of standard extension methods) to ask questions of an enumeration?
For example is the current item the first or last item in the enumeration:
string s = "";
foreach (var person in PeopleListEnumerator) {
if (PeopleListEnumerator.IsFirstItem) s += "[";
s += person.ToString();
if (!PeopleListEnumerator.IsLastItem) s += ",";
else s += "]";
}
Just for the sake of fun, a solution to the general problem that doesn’t require eager evaluation and has a single local variable (except the enumerator):
Test: