public void CopyTo(KeyValuePair <TKey, TValue>[] array, int arrayIndex) { if (_dictionary != null) { // Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations. IDictionaryEnumerator e = _dictionary.GetEnumerator(); try { while (e.MoveNext()) { DictionaryEntry entry = e.Entry; array[arrayIndex++] = new KeyValuePair <TKey, TValue>( (TKey)entry.Key, (TValue)entry.Value ); } } finally { (e as IDisposable)?.Dispose(); } } #if HAVE_READ_ONLY_COLLECTIONS else if (_readOnlyDictionary != null) { throw new NotSupportedException(); } #endif else { GenericDictionary.CopyTo(array, arrayIndex); } }
void ICollection.CopyTo(Array array, int index) { if (_dictionary != null) { _dictionary.CopyTo(array, index); } #if HAVE_READ_ONLY_COLLECTIONS else if (_readOnlyDictionary != null) { throw new NotSupportedException(); } #endif else { GenericDictionary.CopyTo((KeyValuePair <TKey, TValue>[])array, index); } }