@@ -114,12 +114,16 @@ IEnumerator IEnumerable.GetEnumerator()
114
114
115
115
void ICollection . CopyTo ( Array array , int index )
116
116
{
117
- if ( ! ( array is T [ ] typedArray ) )
117
+ if ( array is T [ ] typedArray )
118
118
{
119
- throw new ArgumentException ( $ "Array must be of type { typeof ( T [ ] ) } .", nameof ( array ) ) ;
119
+ CopyTo ( typedArray , index ) ;
120
+ return ;
120
121
}
121
122
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 ) ) ;
123
127
}
124
128
125
129
public int Count => _values . Count + ( _hasNull ? 1 : 0 ) ;
@@ -153,12 +157,26 @@ protected SetSnapShot(SerializationInfo info, StreamingContext context) : base(i
153
157
154
158
void ICollection . CopyTo ( Array array , int index )
155
159
{
156
- if ( ! ( array is T [ ] typedArray ) )
160
+ if ( array is T [ ] typedArray )
157
161
{
158
- throw new ArgumentException ( $ "Array must be of type { typeof ( T [ ] ) } .", nameof ( array ) ) ;
162
+ CopyTo ( typedArray , index ) ;
163
+ return ;
159
164
}
160
165
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
+ }
162
180
}
163
181
164
182
bool ICollection . IsSynchronized => false ;
0 commit comments