When working with a sequence, it is often necessary to determine the total number of elements before iterating through it. However, IEnumerable
To achieve item counting without iteration, consider the following:
Given the following Tables property:
private IEnumerable<string> Tables { get { yield return "Foo"; yield return "Bar"; } }
Without iteration, you can use ICollection
var count = (Tables as ICollection<string>).Count; Console.WriteLine($"You'll process {count} tables");
The above is the detailed content of How Can I Count Items in an IEnumerable Without Iteration?. For more information, please follow other related articles on the PHP Chinese website!