private IEnumerable <TWorksheet> ReadWorksheetsWithCache() { // Iterate TWorkbook.ReadWorksheets() only once and cache the // worksheet instances, which are expensive to create. if (_cachedWorksheets != null) { foreach (var worksheet in _cachedWorksheets) { yield return(worksheet); } if (_cachedWorksheetIterator == null) { yield break; } } else { _cachedWorksheets = new List <TWorksheet>(); } if (_cachedWorksheetIterator == null) { _cachedWorksheetIterator = Workbook.ReadWorksheets().GetEnumerator(); } while (_cachedWorksheetIterator.MoveNext()) { _cachedWorksheets.Add(_cachedWorksheetIterator.Current); yield return(_cachedWorksheetIterator.Current); } _cachedWorksheetIterator.Dispose(); _cachedWorksheetIterator = null; }