Is there some rare language construct I haven\'t encountered (like the few I\'ve learned recently, some on Stack Overflow) in C# to get a value representing the current iter
This answer: lobby the C# language team for direct language support.
The leading answer states:
Obviously, the concept of an index is foreign to the concept of enumeration, and cannot be done.
While this is true of the current C# language version (2020), this is not a conceptual CLR/Language limit, it can be done.
The Microsoft C# language development team could create a new C# language feature, by adding support for a new Interface IIndexedEnumerable
foreach (var item in collection with var index)
{
Console.WriteLine("Iteration {0} has value {1}", index, item);
}
//or, building on @user1414213562's answer
foreach (var (item, index) in collection)
{
Console.WriteLine("Iteration {0} has value {1}", index, item);
}
If foreach ()
is used and with var index
is present, then the compiler expects the item collection to declare IIndexedEnumerable
interface. If the interface is absent, the compiler can polyfill wrap the source with an IndexedEnumerable object, which adds in the code for tracking the index.
interface IIndexedEnumerable : IEnumerable
{
//Not index, because sometimes source IEnumerables are transient
public long IterationNumber { get; }
}
Later, the CLR can be updated to have internal index tracking, that is only used if with
keyword is specified and the source doesn't directly implement IIndexedEnumerable
Why:
While most people here are not Microsoft employees, this is a correct answer, you can lobby Microsoft to add such a feature. You could already build your own iterator with an extension function and use tuples, but Microsoft could sprinkle the syntactic sugar to avoid the extension function