@@ -114,12 +114,16 @@ IEnumerator IEnumerable.GetEnumerator()
114114
115115 void ICollection . CopyTo ( Array array , int index )
116116 {
117- if ( ! ( array is T [ ] typedArray ) )
117+ if ( array is T [ ] typedArray )
118118 {
119- throw new ArgumentException ( $ "Array must be of type { typeof ( T [ ] ) } .", nameof ( array ) ) ;
119+ CopyTo ( typedArray , index ) ;
120+ return ;
120121 }
121122
122- CopyTo ( typedArray , index ) ;
123+ if ( _hasNull )
124+ array . SetValue ( default ( T ) , index ) ;
125+ ICollection keysCollection = _values . Keys ;
126+ keysCollection . CopyTo ( array , index + ( _hasNull ? 1 : 0 ) ) ;
123127 }
124128
125129 public int Count => _values . Count + ( _hasNull ? 1 : 0 ) ;
@@ -153,12 +157,26 @@ protected SetSnapShot(SerializationInfo info, StreamingContext context) : base(i
153157
154158 void ICollection . CopyTo ( Array array , int index )
155159 {
156- if ( ! ( array is T [ ] typedArray ) )
160+ if ( array is T [ ] typedArray )
157161 {
158- throw new ArgumentException ( $ "Array must be of type { typeof ( T [ ] ) } .", nameof ( array ) ) ;
162+ CopyTo ( typedArray , index ) ;
163+ return ;
159164 }
160165
161- CopyTo ( typedArray , index ) ;
166+ if ( array == null )
167+ throw new ArgumentNullException ( nameof ( array ) ) ;
168+
169+ if ( index < 0 )
170+ throw new ArgumentOutOfRangeException ( nameof ( index ) , index , "Array index cannot be negative" ) ;
171+
172+ if ( index > array . Length || Count > array . Length - index )
173+ throw new ArgumentException ( "Provided array is too small" , nameof ( array ) ) ;
174+
175+ foreach ( var value in this )
176+ {
177+ array . SetValue ( value , index ) ;
178+ index ++ ;
179+ }
162180 }
163181
164182 bool ICollection . IsSynchronized => false ;
0 commit comments