d699446f2b5ce3aad52382b8900a6bde0a3161bd
[mono.git] / mcs / class / corlib / System / Array.cs
1 //
2 // System.Array.cs
3 //
4 // Authors:
5 //   Joe Shaw (joe@ximian.com)
6 //   Martin Baulig (martin@gnome.org)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 //   Jeffrey Stedfast (fejj@novell.com)
10 //   Marek Safar (marek.safar@gmail.com)
11 //
12 // (C) 2001-2003 Ximian, Inc.  http://www.ximian.com
13 // Copyright (C) 2004-2011 Novell, Inc (http://www.novell.com)
14 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System.Collections;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39
40 using System.Collections.Generic;
41 using System.Collections.ObjectModel;
42 using System.Runtime.ConstrainedExecution;
43
44 namespace System
45 {
46         public abstract partial class Array
47         {
48                 // Constructor
49                 private Array ()
50                 {
51                 }
52
53                 /*
54                  * These methods are used to implement the implicit generic interfaces 
55                  * implemented by arrays in NET 2.0.
56                  * Only make those methods generic which really need it, to avoid
57                  * creating useless instantiations.
58                  */
59                 internal int InternalArray__ICollection_get_Count ()
60                 {
61                         return Length;
62                 }
63
64                 internal bool InternalArray__ICollection_get_IsReadOnly ()
65                 {
66                         return true;
67                 }
68
69                 internal IEnumerator<T> InternalArray__IEnumerable_GetEnumerator<T> ()
70                 {
71                         return new InternalEnumerator<T> (this);
72                 }
73
74                 internal void InternalArray__ICollection_Clear ()
75                 {
76                         throw new NotSupportedException ("Collection is read-only");
77                 }
78
79                 internal void InternalArray__ICollection_Add<T> (T item)
80                 {
81                         throw new NotSupportedException ("Collection is of a fixed size");
82                 }
83
84                 internal bool InternalArray__ICollection_Remove<T> (T item)
85                 {
86                         throw new NotSupportedException ("Collection is of a fixed size");
87                 }
88
89                 internal bool InternalArray__ICollection_Contains<T> (T item)
90                 {
91                         if (this.Rank > 1)
92                                 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
93
94                         int length = this.Length;
95                         for (int i = 0; i < length; i++) {
96                                 T value;
97                                 GetGenericValueImpl (i, out value);
98                                 if (item == null){
99                                         if (value == null) {
100                                                 return true;
101                                         }
102
103                                         continue;
104                                 }
105
106                                 if (item.Equals (value)) {
107                                         return true;
108                                 }
109                         }
110
111                         return false;
112                 }
113
114                 internal void InternalArray__ICollection_CopyTo<T> (T[] array, int arrayIndex)
115                 {
116                         Copy (this, GetLowerBound (0), array, arrayIndex, Length);
117                 }
118
119                 internal T InternalArray__IReadOnlyList_get_Item<T> (int index)
120                 {
121                         if (unchecked ((uint) index) >= unchecked ((uint) Length))
122                                 throw new ArgumentOutOfRangeException ("index");
123
124                         T value;
125                         GetGenericValueImpl (index, out value);
126                         return value;
127                 }
128
129                 internal int InternalArray__IReadOnlyCollection_get_Count ()
130                 {
131                         return Length;
132                 }
133
134                 internal void InternalArray__Insert<T> (int index, T item)
135                 {
136                         throw new NotSupportedException ("Collection is of a fixed size");
137                 }
138
139                 internal void InternalArray__RemoveAt (int index)
140                 {
141                         throw new NotSupportedException ("Collection is of a fixed size");
142                 }
143
144                 internal int InternalArray__IndexOf<T> (T item)
145                 {
146                         if (this.Rank > 1)
147                                 throw new RankException (Locale.GetText ("Only single dimension arrays are supported."));
148
149                         int length = this.Length;
150                         for (int i = 0; i < length; i++) {
151                                 T value;
152                                 GetGenericValueImpl (i, out value);
153                                 if (item == null){
154                                         if (value == null)
155                                                 return i + this.GetLowerBound (0);
156
157                                         continue;
158                                 }
159                                 if (value.Equals (item))
160                                         // array index may not be zero-based.
161                                         // use lower bound
162                                         return i + this.GetLowerBound (0);
163                         }
164
165                         unchecked {
166                                 // lower bound may be MinValue
167                                 return this.GetLowerBound (0) - 1;
168                         }
169                 }
170
171                 internal T InternalArray__get_Item<T> (int index)
172                 {
173                         if (unchecked ((uint) index) >= unchecked ((uint) Length))
174                                 throw new ArgumentOutOfRangeException ("index");
175
176                         T value;
177                         GetGenericValueImpl (index, out value);
178                         return value;
179                 }
180
181                 internal void InternalArray__set_Item<T> (int index, T item)
182                 {
183                         if (unchecked ((uint) index) >= unchecked ((uint) Length))
184                                 throw new ArgumentOutOfRangeException ("index");
185
186                         object[] oarray = this as object [];
187                         if (oarray != null) {
188                                 oarray [index] = (object)item;
189                                 return;
190                         }
191                         SetGenericValueImpl (index, ref item);
192                 }
193
194                 // CAUTION! No bounds checking!
195                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
196                 internal extern void GetGenericValueImpl<T> (int pos, out T value);
197
198                 // CAUTION! No bounds checking!
199                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
200                 internal extern void SetGenericValueImpl<T> (int pos, ref T value);
201
202                 internal struct InternalEnumerator<T> : IEnumerator<T>
203                 {
204                         const int NOT_STARTED = -2;
205                         
206                         // this MUST be -1, because we depend on it in move next.
207                         // we just decr the size, so, 0 - 1 == FINISHED
208                         const int FINISHED = -1;
209                         
210                         readonly Array array;
211                         int idx;
212
213                         internal InternalEnumerator (Array array)
214                         {
215                                 this.array = array;
216                                 idx = NOT_STARTED;
217                         }
218
219                         public void Dispose ()
220                         {
221                         }
222
223                         public bool MoveNext ()
224                         {
225                                 if (idx == NOT_STARTED)
226                                         idx = array.Length;
227
228                                 return idx != FINISHED && -- idx != FINISHED;
229                         }
230
231                         public T Current {
232                                 get {
233                                         if (idx == NOT_STARTED)
234                                                 throw new InvalidOperationException ("Enumeration has not started. Call MoveNext");
235                                         if (idx == FINISHED)
236                                                 throw new InvalidOperationException ("Enumeration already finished");
237
238                                         return array.InternalArray__get_Item<T> (array.Length - 1 - idx);
239                                 }
240                         }
241
242                         void IEnumerator.Reset ()
243                         {
244                                 idx = NOT_STARTED;
245                         }
246
247                         object IEnumerator.Current {
248                                 get {
249                                         return Current;
250                                 }
251                         }
252                 }
253
254                 // Properties
255                 public int Length {
256                         [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
257                         get {
258                                 int length = this.GetLength (0);
259
260                                 for (int i = 1; i < this.Rank; i++) {
261                                         length *= this.GetLength (i); 
262                                 }
263                                 return length;
264                         }
265                 }
266
267                 public int Rank {
268                         [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
269                         get {
270                                 return this.GetRank ();
271                         }
272                 }
273
274                 // InternalCall Methods
275                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
276                 extern int GetRank ();
277
278                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
279                 public extern int GetLength (int dimension);
280
281                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
282                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
283                 public extern int GetLowerBound (int dimension);
284
285                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
286                 public extern object GetValue (params int[] indices);
287
288                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
289                 public extern void SetValue (object value, params int[] indices);
290
291                 // CAUTION! No bounds checking!
292                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
293                 internal extern object GetValueImpl (int pos);
294
295                 // CAUTION! No bounds checking!
296                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
297                 internal extern void SetValueImpl (object value, int pos);
298
299                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
300                 internal extern static bool FastCopy (Array source, int source_idx, Array dest, int dest_idx, int length);
301
302                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
303                 internal extern static Array CreateInstanceImpl (Type elementType, int[] lengths, int[] bounds);
304
305                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
306                 public int GetUpperBound (int dimension)
307                 {
308                         return GetLowerBound (dimension) + GetLength (dimension) - 1;
309                 }
310
311                 public object GetValue (int index)
312                 {
313                         if (Rank != 1)
314                                 throw new ArgumentException (SR.Arg_RankMultiDimNotSupported);
315
316                         var lb  = GetLowerBound (0);
317                         if (index < lb || index > GetUpperBound (0))
318                                 throw new IndexOutOfRangeException (Locale.GetText (
319                                         "Index has to be between upper and lower bound of the array."));
320
321                         if (GetType ().GetElementType ().IsPointer)
322                                 throw new NotSupportedException ("Type is not supported");
323
324                         return GetValueImpl (index - lb);
325                 }
326
327                 public object GetValue (int index1, int index2)
328                 {
329                         int[] ind = {index1, index2};
330                         return GetValue (ind);
331                 }
332
333                 public object GetValue (int index1, int index2, int index3)
334                 {
335                         int[] ind = {index1, index2, index3};
336                         return GetValue (ind);
337                 }
338
339                 public void SetValue (object value, int index)
340                 {
341                         if (Rank != 1)
342                                 throw new ArgumentException (SR.Arg_RankMultiDimNotSupported);
343
344                         var lb  = GetLowerBound (0);
345                         if (index < lb || index > GetUpperBound (0))
346                                 throw new IndexOutOfRangeException (Locale.GetText (
347                                         "Index has to be >= lower bound and <= upper bound of the array."));
348
349                         if (GetType ().GetElementType ().IsPointer)
350                                 throw new NotSupportedException ("Type is not supported");
351
352                         SetValueImpl (value, index - lb);
353                 }
354
355                 public void SetValue (object value, int index1, int index2)
356                 {
357                         int[] ind = {index1, index2};
358                         SetValue (value, ind);
359                 }
360
361                 public void SetValue (object value, int index1, int index2, int index3)
362                 {
363                         int[] ind = {index1, index2, index3};
364                         SetValue (value, ind);
365                 }
366
367                 internal static Array UnsafeCreateInstance(Type elementType, int[] lengths, int[] lowerBounds)
368                 {
369                         return CreateInstance(elementType, lengths, lowerBounds);
370                 }
371
372                 internal static Array UnsafeCreateInstance (Type elementType, int length1, int length2)
373                 {
374                         return CreateInstance (elementType, length1, length2);
375                 }
376
377                 internal static Array UnsafeCreateInstance (Type elementType, params int[] lengths)
378                 {
379                         return CreateInstance(elementType, lengths);
380                 }
381
382                 public static Array CreateInstance (Type elementType, int length)
383                 {
384                         int[] lengths = {length};
385
386                         return CreateInstance (elementType, lengths);
387                 }
388
389                 public static Array CreateInstance (Type elementType, int length1, int length2)
390                 {
391                         int[] lengths = {length1, length2};
392
393                         return CreateInstance (elementType, lengths);
394                 }
395
396                 public static Array CreateInstance (Type elementType, int length1, int length2, int length3)
397                 {
398                         int[] lengths = {length1, length2, length3};
399
400                         return CreateInstance (elementType, lengths);
401                 }
402
403                 public static Array CreateInstance (Type elementType, params int[] lengths)
404                 {
405                         if (elementType == null)
406                                 throw new ArgumentNullException ("elementType");
407                         if (lengths == null)
408                                 throw new ArgumentNullException ("lengths");
409
410                         if (lengths.Length > 255)
411                                 throw new TypeLoadException ();
412
413                         int[] bounds = null;
414
415                         elementType = elementType.UnderlyingSystemType as RuntimeType;
416                         if (elementType == null)
417                                 throw new ArgumentException ("Type must be a type provided by the runtime.", "elementType");
418                         if (elementType.Equals (typeof (void)))
419                                 throw new NotSupportedException ("Array type can not be void");
420                         if (elementType.ContainsGenericParameters)
421                                 throw new NotSupportedException ("Array type can not be an open generic type");
422                         
423                         return CreateInstanceImpl (elementType, lengths, bounds);
424                 }
425
426                 public static Array CreateInstance (Type elementType, int[] lengths, int [] lowerBounds)
427                 {
428                         if (elementType == null)
429                                 throw new ArgumentNullException ("elementType");
430                         if (lengths == null)
431                                 throw new ArgumentNullException ("lengths");
432                         if (lowerBounds == null)
433                                 throw new ArgumentNullException ("lowerBounds");
434
435                         elementType = elementType.UnderlyingSystemType as RuntimeType;
436                         if (elementType == null)
437                                 throw new ArgumentException ("Type must be a type provided by the runtime.", "elementType");
438                         if (elementType.Equals (typeof (void)))
439                                 throw new NotSupportedException ("Array type can not be void");
440                         if (elementType.ContainsGenericParameters)
441                                 throw new NotSupportedException ("Array type can not be an open generic type");
442
443                         if (lengths.Length < 1)
444                                 throw new ArgumentException (Locale.GetText ("Arrays must contain >= 1 elements."));
445
446                         if (lengths.Length != lowerBounds.Length)
447                                 throw new ArgumentException (Locale.GetText ("Arrays must be of same size."));
448
449                         for (int j = 0; j < lowerBounds.Length; j ++) {
450                                 if (lengths [j] < 0)
451                                         throw new ArgumentOutOfRangeException ("lengths", Locale.GetText (
452                                                 "Each value has to be >= 0."));
453                                 if ((long)lowerBounds [j] + (long)lengths [j] > (long)Int32.MaxValue)
454                                         throw new ArgumentOutOfRangeException ("lengths", Locale.GetText (
455                                                 "Length + bound must not exceed Int32.MaxValue."));
456                         }
457
458                         if (lengths.Length > 255)
459                                 throw new TypeLoadException ();
460
461                         return CreateInstanceImpl (elementType, lengths, lowerBounds);
462                 }
463
464                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
465                 public static void Clear (Array array, int index, int length)
466                 {
467                         if (array == null)
468                                 throw new ArgumentNullException ("array");
469                         if (length < 0)
470                                 throw new IndexOutOfRangeException ("length < 0");
471
472                         int low = array.GetLowerBound (0);
473                         if (index < low)
474                                 throw new IndexOutOfRangeException ("index < lower bound");
475                         index = index - low;
476
477                         // re-ordered to avoid possible integer overflow
478                         if (index > array.Length - length)
479                                 throw new IndexOutOfRangeException ("index + length > size");
480
481                         ClearInternal (array, index, length);
482                 }
483                 
484                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
485                 static extern void ClearInternal (Array a, int index, int count);
486
487                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
488                 public static void Copy (Array sourceArray, Array destinationArray, int length)
489                 {
490                         // need these checks here because we are going to use
491                         // GetLowerBound() on source and dest.
492                         if (sourceArray == null)
493                                 throw new ArgumentNullException ("sourceArray");
494
495                         if (destinationArray == null)
496                                 throw new ArgumentNullException ("destinationArray");
497
498                         Copy (sourceArray, sourceArray.GetLowerBound (0), destinationArray,
499                                 destinationArray.GetLowerBound (0), length);
500                 }
501
502                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
503                 public static void Copy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
504                 {
505                         if (sourceArray == null)
506                                 throw new ArgumentNullException ("sourceArray");
507
508                         if (destinationArray == null)
509                                 throw new ArgumentNullException ("destinationArray");
510
511                         if (length < 0)
512                                 throw new ArgumentOutOfRangeException ("length", Locale.GetText (
513                                         "Value has to be >= 0."));;
514
515                         if (sourceArray.Rank != destinationArray.Rank)
516                                 throw new RankException(SR.Rank_MultiDimNotSupported);
517
518                         if (sourceIndex < 0)
519                                 throw new ArgumentOutOfRangeException ("sourceIndex", Locale.GetText (
520                                         "Value has to be >= 0."));;
521
522                         if (destinationIndex < 0)
523                                 throw new ArgumentOutOfRangeException ("destinationIndex", Locale.GetText (
524                                         "Value has to be >= 0."));;
525
526                         if (FastCopy (sourceArray, sourceIndex, destinationArray, destinationIndex, length))
527                                 return;
528
529                         int source_pos = sourceIndex - sourceArray.GetLowerBound (0);
530                         int dest_pos = destinationIndex - destinationArray.GetLowerBound (0);
531
532                         if (dest_pos < 0)
533                                 throw new ArgumentOutOfRangeException ("destinationIndex", "Index was less than the array's lower bound in the first dimension.");
534
535                         // re-ordered to avoid possible integer overflow
536                         if (source_pos > sourceArray.Length - length)
537                                 throw new ArgumentException ("length");
538
539                         if (dest_pos > destinationArray.Length - length) {
540                                 string msg = "Destination array was not long enough. Check " +
541                                         "destIndex and length, and the array's lower bounds";
542                                 throw new ArgumentException (msg, string.Empty);
543                         }
544
545                         Type src_type = sourceArray.GetType ().GetElementType ();
546                         Type dst_type = destinationArray.GetType ().GetElementType ();
547
548                         if (!Object.ReferenceEquals (sourceArray, destinationArray) || source_pos > dest_pos) {
549                                 for (int i = 0; i < length; i++) {
550                                         Object srcval = sourceArray.GetValueImpl (source_pos + i);
551
552                                         try {
553                                                 destinationArray.SetValueImpl (srcval, dest_pos + i);
554                                         } catch (ArgumentException) {
555                                                 throw CreateArrayTypeMismatchException ();
556                                         } catch {
557                                                 if (CanAssignArrayElement (src_type, dst_type))
558                                                         throw;
559
560                                                 throw CreateArrayTypeMismatchException ();
561                                         }
562                                 }
563                         }
564                         else {
565                                 for (int i = length - 1; i >= 0; i--) {
566                                         Object srcval = sourceArray.GetValueImpl (source_pos + i);
567
568                                         try {
569                                                 destinationArray.SetValueImpl (srcval, dest_pos + i);
570                                         } catch (ArgumentException) {
571                                                 throw CreateArrayTypeMismatchException ();
572                                         } catch {
573                                                 if (CanAssignArrayElement (src_type, dst_type))
574                                                         throw;
575
576                                                 throw CreateArrayTypeMismatchException ();
577                                         }
578                                 }
579                         }
580                 }
581
582                 static Exception CreateArrayTypeMismatchException ()
583                 {
584                         return new ArrayTypeMismatchException ();
585                 }
586
587                 static bool CanAssignArrayElement (Type source, Type target)
588                 {
589                         if (source.IsValueType)
590                                 return source.IsAssignableFrom (target);
591
592                         if (source.IsInterface)
593                                 return !target.IsValueType;
594
595                         if (target.IsInterface)
596                                 return !source.IsValueType;
597
598                         return source.IsAssignableFrom (target) || target.IsAssignableFrom (source);
599                 }
600
601                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
602                 //
603                 // The constrained copy should guarantee that if there is an exception thrown
604                 // during the copy, the destination array remains unchanged.
605                 // This is related to System.Runtime.Reliability.CER
606                 public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length)
607                 {
608                         Copy (sourceArray, sourceIndex, destinationArray, destinationIndex, length);
609                 }
610
611                 public static T[] Empty<T>()
612                 {
613                         return EmptyArray<T>.Value;
614                 }
615
616                 public void Initialize()
617                 {
618                         return;
619                 }
620
621                 static int IndexOfImpl<T>(T[] array, T value, int startIndex, int count)
622                 {
623                         return EqualityComparer<T>.Default.IndexOf (array, value, startIndex, count);
624                 }
625
626                 static int LastIndexOfImpl<T>(T[] array, T value, int startIndex, int count)
627                 {
628                         return EqualityComparer<T>.Default.LastIndexOf (array, value, startIndex, count);
629                 }
630
631                 static void SortImpl (Array keys, Array items, int index, int length, IComparer comparer)
632                 {
633                         Object[] objKeys = keys as Object[];
634                         Object[] objItems = null;
635                         if (objKeys != null)
636                                 objItems = items as Object[];
637
638                         if (objKeys != null && (items == null || objItems != null)) {
639                                 SorterObjectArray sorter = new SorterObjectArray(objKeys, objItems, comparer);
640                                 sorter.Sort(index, length);
641                         } else {
642                                 SorterGenericArray sorter = new SorterGenericArray(keys, items, comparer);
643                                 sorter.Sort(index, length);
644                         }
645                 }
646
647                 #region Unsafe array operations
648
649                 //
650                 // Loads array index with no safety checks (JIT intristics)
651                 //
652                 internal static T UnsafeLoad<T> (T[] array, int index) {
653                         return array [index];
654                 }
655
656                 //
657                 // Stores values at specified array index with no safety checks (JIT intristics)
658                 //
659                 internal static void UnsafeStore<T> (T[] array, int index, T value) {
660                         array [index] = value;
661                 }
662
663                 //
664                 // Moved value from instance into target of different type with no checks (JIT intristics)
665                 //
666                 // Restrictions:
667                 //
668                 // S and R must either:
669                 //       both be blitable valuetypes
670                 //       both be reference types (IOW, an unsafe cast)
671                 // S and R cannot be float or double
672                 // S and R must either:
673                 //       both be a struct
674                 //       both be a scalar
675                 // S and R must either:
676                 //       be of same size
677                 //       both be a scalar of size <= 4
678                 //
679                 internal static R UnsafeMov<S,R> (S instance) {
680                         return (R)(object) instance;
681                 }
682
683                 #endregion
684
685                 internal sealed class FunctorComparer<T> : IComparer<T> {
686                         Comparison<T> comparison;
687
688                         public FunctorComparer(Comparison<T> comparison) {
689                                 this.comparison = comparison;
690                         }
691
692                         public int Compare(T x, T y) {
693                                 return comparison(x, y);
694                         }
695                 }
696
697                 partial class ArrayEnumerator
698                 {
699                         public Object Current {
700                                 get {
701                                         if (_index < 0) throw new InvalidOperationException (SR.InvalidOperation_EnumNotStarted);
702                                         if (_index >= _endIndex) throw new InvalidOperationException (SR.InvalidOperation_EnumEnded);
703                                         if (_index == 0 && _array.GetType ().GetElementType ().IsPointer) throw new NotSupportedException ("Type is not supported");
704                                         return _array.GetValueImpl(_index);
705                                 }
706                         }
707                 }
708         }
709 }