Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / System.Collections.Generic / List.cs
1 //
2 // System.Collections.Generic.List
3 //
4 // Authors:
5 //    Ben Maurer (bmaurer@ximian.com)
6 //    Martin Baulig (martin@ximian.com)
7 //    Carlos Alberto Cortez (calberto.cortez@gmail.com)
8 //    David Waite (mass@akuma.org)
9 //    Marek Safar (marek.safar@gmail.com)
10 //
11 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Copyright (C) 2005 David Waite
13 // Copyright (C) 2011,2012 Xamarin, Inc (http://www.xamarin.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections.ObjectModel;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics;
38 using System.Runtime.CompilerServices;
39
40 namespace System.Collections.Generic {
41         [Serializable]
42         [DebuggerDisplay ("Count={Count}")]
43         [DebuggerTypeProxy (typeof (CollectionDebuggerView<>))]
44         public class List<T> : IList<T>, IList
45 #if NET_4_5
46                 , IReadOnlyList<T>
47 #endif
48         {
49                 T [] _items;
50                 int _size;
51                 int _version;
52                 
53                 const int DefaultCapacity = 4;
54                 
55                 public List ()
56                 {
57                         _items = EmptyArray<T>.Value;
58                 }
59                 
60                 public List (IEnumerable <T> collection)
61                 {
62                         if (collection == null)
63                                 throw new ArgumentNullException ("collection");
64
65                         // initialize to needed size (if determinable)
66                         ICollection <T> c = collection as ICollection <T>;
67                         if (c == null) {
68                                 _items = EmptyArray<T>.Value;;
69                                 AddEnumerable (collection);
70                         } else {
71                                 _size = c.Count;
72                                 _items = new T [_size];
73                                 c.CopyTo (_items, 0);
74                         }
75                 }
76                 
77                 public List (int capacity)
78                 {
79                         if (capacity < 0)
80                                 throw new ArgumentOutOfRangeException ("capacity");
81                         _items = new T [capacity];
82                 }
83                 
84                 internal List (T [] data, int size)
85                 {
86                         _items = data;
87                         _size = size;
88                 }
89                 
90                 public void Add (T item)
91                 {
92                         // If we check to see if we need to grow before trying to grow
93                         // we can speed things up by 25%
94                         if (_size == _items.Length)
95                                 GrowIfNeeded (1);
96                         _items [_size++] = item;
97                         _version++;
98                 }
99                 
100                 void GrowIfNeeded (int newCount)
101                 {
102                         int minimumSize = _size + newCount;
103                         if (minimumSize > _items.Length)
104                                 Capacity = Math.Max (Math.Max (Capacity * 2, DefaultCapacity), minimumSize);
105                 }
106                 
107                 void CheckRange (int idx, int count)
108                 {
109                         if (idx < 0)
110                                 throw new ArgumentOutOfRangeException ("index");
111                         
112                         if (count < 0)
113                                 throw new ArgumentOutOfRangeException ("count");
114
115                         if ((uint) idx + (uint) count > (uint) _size)
116                                 throw new ArgumentException ("index and count exceed length of list");
117                 }
118                 
119                 void CheckRangeOutOfRange (int idx, int count)
120                 {
121                         if (idx < 0)
122                                 throw new ArgumentOutOfRangeException ("index");
123                         
124                         if (count < 0)
125                                 throw new ArgumentOutOfRangeException ("count");
126
127                         if ((uint) idx + (uint) count > (uint) _size)
128                                 throw new ArgumentOutOfRangeException ("index and count exceed length of list");
129                 }
130
131                 void AddCollection (ICollection <T> collection)
132                 {
133                         int collectionCount = collection.Count;
134                         if (collectionCount == 0)
135                                 return;
136
137                         GrowIfNeeded (collectionCount);                  
138                         collection.CopyTo (_items, _size);
139                         _size += collectionCount;
140                 }
141
142                 void AddEnumerable (IEnumerable <T> enumerable)
143                 {
144                         foreach (T t in enumerable)
145                         {
146                                 Add (t);
147                         }
148                 }
149
150                 public void AddRange (IEnumerable <T> collection)
151                 {
152                         if (collection == null)
153                                 throw new ArgumentNullException ("collection");
154                         
155                         ICollection <T> c = collection as ICollection <T>;
156                         if (c != null)
157                                 AddCollection (c);
158                         else
159                                 AddEnumerable (collection);
160                         _version++;
161                 }
162                 
163                 public ReadOnlyCollection <T> AsReadOnly ()
164                 {
165                         return new ReadOnlyCollection <T> (this);
166                 }
167                 
168                 public int BinarySearch (T item)
169                 {
170                         return Array.BinarySearch <T> (_items, 0, _size, item);
171                 }
172                 
173                 public int BinarySearch (T item, IComparer <T> comparer)
174                 {
175                         return Array.BinarySearch <T> (_items, 0, _size, item, comparer);
176                 }
177                 
178                 public int BinarySearch (int index, int count, T item, IComparer <T> comparer)
179                 {
180                         CheckRange (index, count);
181                         return Array.BinarySearch <T> (_items, index, count, item, comparer);
182                 }
183                 
184                 public void Clear ()
185                 {
186                         Array.Clear (_items, 0, _items.Length);
187                         _size = 0;
188                         _version++;
189                 }
190                 
191                 public bool Contains (T item)
192                 {
193                         return Array.IndexOf<T>(_items, item, 0, _size) != -1;
194                 }
195                 
196                 public List <TOutput> ConvertAll <TOutput> (Converter <T, TOutput> converter)
197                 {
198                         if (converter == null)
199                                 throw new ArgumentNullException ("converter");
200                         List <TOutput> u = new List <TOutput> (_size);
201                         for (int i = 0; i < _size; i++)
202                                 u._items[i] = converter(_items[i]);
203
204                         u._size = _size;
205                         return u;
206                 }
207                 
208                 public void CopyTo (T [] array)
209                 {
210                         Array.Copy (_items, 0, array, 0, _size);
211                 }
212                 
213                 public void CopyTo (T [] array, int arrayIndex)
214                 {
215                         Array.Copy (_items, 0, array, arrayIndex, _size);
216                 }
217                 
218                 public void CopyTo (int index, T [] array, int arrayIndex, int count)
219                 {
220                         CheckRange (index, count);
221                         Array.Copy (_items, index, array, arrayIndex, count);
222                 }
223
224                 public bool Exists (Predicate <T> match)
225                 {
226                         CheckMatch(match);
227
228                         for (int i = 0; i < _size; i++) {
229                                 var item = _items [i];
230                                 if (match (item))
231                                         return true;
232                         }
233
234                         return false;
235                 }
236                 
237                 public T Find (Predicate <T> match)
238                 {
239                         CheckMatch(match);
240
241                         for (int i = 0; i < _size; i++) {
242                                 var item = _items [i];
243                                 if (match (item))
244                                         return item;
245                         }
246
247                         return default (T);
248                 }
249                 
250                 static void CheckMatch (Predicate <T> match)
251                 {
252                         if (match == null)
253                                 throw new ArgumentNullException ("match");
254                 }
255                 
256                 public List <T> FindAll (Predicate <T> match)
257                 {
258                         CheckMatch (match);
259                         if (this._size <= 0x10000) // <= 8 * 1024 * 8 (8k in stack)
260                                 return this.FindAllStackBits (match);
261                         else 
262                                 return this.FindAllList (match);
263                 }
264                 
265                 private List <T> FindAllStackBits (Predicate <T> match)
266                 {
267                         unsafe
268                         {
269                                 uint *bits = stackalloc uint [(this._size / 32) + 1];
270                                 uint *ptr = bits;
271                                 int found = 0;
272                                 uint bitmask = 0x80000000;
273                                 
274                                 for (int i = 0; i < this._size; i++)
275                                 {
276                                         if (match (this._items [i]))
277                                         {
278                                                 (*ptr) = (*ptr) | bitmask;
279                                                 found++;
280                                         }
281                                         
282                                         bitmask = bitmask >> 1;
283                                         if (bitmask == 0)
284                                         {
285                                                 ptr++;
286                                                 bitmask = 0x80000000;
287                                         }
288                                 }
289                                 
290                                 T [] results = new T [found];
291                                 bitmask = 0x80000000;
292                                 ptr = bits;
293                                 int j = 0;
294                                 for (int i = 0; i < this._size && j < found; i++)
295                                 {
296                                         if (((*ptr) & bitmask) == bitmask)
297                                                 results [j++] = this._items [i];
298                                         
299                                         bitmask = bitmask >> 1;
300                                         if (bitmask == 0)
301                                         {
302                                                 ptr++;
303                                                 bitmask = 0x80000000;
304                                         }
305                                 }
306                                 
307                                 return new List <T> (results, found);
308                         }
309                 }
310                 
311                 private List <T> FindAllList (Predicate <T> match)
312                 {
313                         List <T> results = new List <T> ();
314                         for (int i = 0; i < this._size; i++)
315                                 if (match (this._items [i]))
316                                         results.Add (this._items [i]);
317                         
318                         return results;
319                 }
320                 
321                 public int FindIndex (Predicate <T> match)
322                 {
323                         CheckMatch (match);
324                         return Array.GetIndex (_items, 0, _size, match);
325                 }
326                 
327                 public int FindIndex (int startIndex, Predicate <T> match)
328                 {
329                         CheckMatch (match);
330                         CheckStartIndex (startIndex);
331                         return Array.GetIndex (_items, startIndex, _size - startIndex, match);
332                 }
333                 public int FindIndex (int startIndex, int count, Predicate <T> match)
334                 {
335                         CheckMatch (match);
336                         CheckRangeOutOfRange (startIndex, count);
337                         return Array.GetIndex (_items, startIndex, count, match);
338                 }
339                 
340                 public T FindLast (Predicate <T> match)
341                 {
342                         CheckMatch (match);
343                         int i = Array.GetLastIndex (_items, 0, _size, match);
344                         return i == -1 ? default (T) : this [i];
345                 }
346                 
347                 public int FindLastIndex (Predicate <T> match)
348                 {
349                         CheckMatch (match);
350                         return Array.GetLastIndex (_items, 0, _size, match);
351                 }
352                 
353                 public int FindLastIndex (int startIndex, Predicate <T> match)
354                 {
355                         CheckMatch (match);
356                         CheckStartIndex (startIndex);
357                         return Array.GetLastIndex (_items, 0, startIndex + 1, match);
358                 }
359                 
360                 public int FindLastIndex (int startIndex, int count, Predicate <T> match)
361                 {
362                         CheckMatch (match);
363                         CheckStartIndex (startIndex);
364                         
365                         if (count < 0)
366                                 throw new ArgumentOutOfRangeException ("count");
367
368                         if (startIndex - count + 1 < 0)
369                                 throw new ArgumentOutOfRangeException ("count must refer to a location within the collection");
370
371                         return Array.GetLastIndex (_items, startIndex - count + 1, count, match);
372                 }
373
374                 public void ForEach (Action <T> action)
375                 {
376                         if (action == null)
377                                 throw new ArgumentNullException ("action");
378                         for(int i=0; i < _size; i++)
379                                 action(_items[i]);
380                 }
381                 
382                 public Enumerator GetEnumerator ()
383                 {
384                         return new Enumerator (this);
385                 }
386                 
387                 public List <T> GetRange (int index, int count)
388                 {
389                         CheckRange (index, count);
390                         T [] tmpArray = new T [count];
391                         Array.Copy (_items, index, tmpArray, 0, count);
392                         return new List <T> (tmpArray, count);
393                 }
394                 
395                 public int IndexOf (T item)
396                 {
397                         return Array.IndexOf<T> (_items, item, 0, _size);
398                 }
399                 
400                 public int IndexOf (T item, int index)
401                 {
402                         CheckIndex (index);
403                         return Array.IndexOf<T> (_items, item, index, _size - index);
404                 }
405                 
406                 public int IndexOf (T item, int index, int count)
407                 {
408                         if (index < 0)
409                                 throw new ArgumentOutOfRangeException ("index");
410                         
411                         if (count < 0)
412                                 throw new ArgumentOutOfRangeException ("count");
413
414                         if ((uint) index + (uint) count > (uint) _size)
415                                 throw new ArgumentOutOfRangeException ("index and count exceed length of list");
416
417                         return Array.IndexOf<T> (_items, item, index, count);
418                 }
419                 
420                 void Shift (int start, int delta)
421                 {
422                         if (delta < 0)
423                                 start -= delta;
424                         
425                         if (start < _size)
426                                 Array.Copy (_items, start, _items, start + delta, _size - start);
427                         
428                         _size += delta;
429
430                         if (delta < 0)
431                                 Array.Clear (_items, _size, -delta);
432                 }
433
434                 void CheckIndex (int index)
435                 {
436                         if (index < 0 || (uint) index > (uint) _size)
437                                 throw new ArgumentOutOfRangeException ("index");
438                 }
439
440                 void CheckStartIndex (int index)
441                 {
442                         if (index < 0 || (uint) index > (uint) _size)
443                                 throw new ArgumentOutOfRangeException ("startIndex");
444                 }
445                 
446                 public void Insert (int index, T item)
447                 {
448                         CheckIndex (index);                     
449                         if (_size == _items.Length)
450                                 GrowIfNeeded (1);
451                         Shift (index, 1);
452                         _items[index] = item;
453                         _version++;
454                 }
455                 
456                 public void InsertRange (int index, IEnumerable <T> collection)
457                 {
458                         if (collection == null)
459                                 throw new ArgumentNullException ("collection");
460
461                         CheckIndex (index);
462                         if (collection == this) {
463                                 T[] buffer = new T[_size];
464                                 CopyTo (buffer, 0);
465                                 GrowIfNeeded (_size);
466                                 Shift (index, buffer.Length);
467                                 Array.Copy (buffer, 0, _items, index, buffer.Length);
468                         } else {
469                                 ICollection <T> c = collection as ICollection <T>;
470                                 if (c != null)
471                                         InsertCollection (index, c);
472                                 else
473                                         InsertEnumeration (index, collection);
474                         }
475                         _version++;
476                 }
477
478                 void InsertCollection (int index, ICollection <T> collection)
479                 {
480                         int collectionCount = collection.Count;
481                         GrowIfNeeded (collectionCount);
482                         
483                         Shift (index, collectionCount);
484                         collection.CopyTo (_items, index);
485                 }
486                 
487                 void InsertEnumeration (int index, IEnumerable <T> enumerable)
488                 {
489                         foreach (T t in enumerable)
490                                 Insert (index++, t);            
491                 }
492
493                 public int LastIndexOf (T item)
494                 {
495                         if (_size == 0)
496                                 return -1;
497                         return Array.LastIndexOf<T> (_items, item, _size - 1, _size);
498                 }
499                 
500                 public int LastIndexOf (T item, int index)
501                 {
502                         CheckIndex (index);
503                         return Array.LastIndexOf<T> (_items, item, index, index + 1);
504                 }
505                 
506                 public int LastIndexOf (T item, int index, int count)
507                 {
508                         if (index < 0)
509                                 throw new ArgumentOutOfRangeException ("index", index, "index is negative");
510
511                         if (count < 0)
512                                 throw new ArgumentOutOfRangeException ("count", count, "count is negative");
513
514                         if (index - count + 1 < 0)
515                                 throw new ArgumentOutOfRangeException ("cound", count, "count is too large");
516
517                         return Array.LastIndexOf<T> (_items, item, index, count);
518                 }
519                 
520                 public bool Remove (T item)
521                 {
522                         int loc = IndexOf (item);
523                         if (loc != -1)
524                                 RemoveAt (loc);
525                         
526                         return loc != -1;
527                 }
528                 
529                 public int RemoveAll (Predicate <T> match)
530                 {
531                         CheckMatch(match);
532                         int i = 0;
533                         int j = 0;
534
535                         // Find the first item to remove
536                         for (i = 0; i < _size; i++)
537                                 if (match(_items[i]))
538                                         break;
539
540                         if (i == _size)
541                                 return 0;
542
543                         _version++;
544
545                         // Remove any additional items
546                         for (j = i + 1; j < _size; j++)
547                         {
548                                 if (!match(_items[j]))
549                                         _items[i++] = _items[j];
550                         }
551                         if (j - i > 0)
552                                 Array.Clear (_items, i, j - i);
553
554                         _size = i;
555                         return (j - i);
556                 }
557                 
558                 public void RemoveAt (int index)
559                 {
560                         if (index < 0 || (uint)index >= (uint)_size)
561                                 throw new ArgumentOutOfRangeException("index");
562                         Shift (index, -1);
563                         Array.Clear (_items, _size, 1);
564                         _version++;
565                 }
566                 
567                 public void RemoveRange (int index, int count)
568                 {
569                         CheckRange (index, count);
570                         if (count > 0) {
571                                 Shift (index, -count);
572                                 Array.Clear (_items, _size, count);
573                                 _version++;
574                         }
575                 }
576                 
577                 public void Reverse ()
578                 {
579                         Array.Reverse (_items, 0, _size);
580                         _version++;
581                 }
582                 public void Reverse (int index, int count)
583                 {
584                         CheckRange (index, count);
585                         Array.Reverse (_items, index, count);
586                         _version++;
587                 }
588                 
589                 public void Sort ()
590                 {
591                         Array.Sort<T> (_items, 0, _size);
592                         _version++;
593                 }
594                 public void Sort (IComparer <T> comparer)
595                 {
596                         Array.Sort<T> (_items, 0, _size, comparer);
597                         _version++;
598                 }
599
600                 public void Sort (Comparison <T> comparison)
601                 {
602                         if (comparison == null)
603                                 throw new ArgumentNullException ("comparison");
604
605                         Array.SortImpl<T> (_items, _size, comparison);
606                         _version++;
607                 }
608                 
609                 public void Sort (int index, int count, IComparer <T> comparer)
610                 {
611                         CheckRange (index, count);
612                         Array.Sort<T> (_items, index, count, comparer);
613                         _version++;
614                 }
615
616                 public T [] ToArray ()
617                 {
618                         T [] t = new T [_size];
619                         Array.Copy (_items, t, _size);
620                         
621                         return t;
622                 }
623                 
624                 public void TrimExcess ()
625                 {
626                         Capacity = _size;
627                 }
628                 
629                 public bool TrueForAll (Predicate <T> match)
630                 {
631                         CheckMatch (match);
632
633                         for (int i = 0; i < _size; i++)
634                                 if (!match(_items[i]))
635                                         return false;
636                                 
637                         return true;
638                 }
639                 
640                 public int Capacity {
641                         get { 
642                                 return _items.Length;
643                         }
644                         set {
645                                 if ((uint) value < (uint) _size)
646                                         throw new ArgumentOutOfRangeException ();
647                                 
648                                 Array.Resize (ref _items, value);
649                         }
650                 }
651                 
652                 public int Count {
653                         get { return _size; }
654                 }
655                 
656                 public T this [int index] {
657                         [MethodImpl ((MethodImplOptions)256)]
658                         get {
659                                 if ((uint) index >= (uint) _size)
660                                         throw new ArgumentOutOfRangeException ("index");
661                                 return Array.UnsafeLoad (_items, index);
662                         }
663
664                         [MethodImpl ((MethodImplOptions)256)]
665                         set {
666                                 if ((uint) index >= (uint) _size)
667                                         throw new ArgumentOutOfRangeException ("index");
668                                 _items [index] = value;
669                                 _version++;
670                         }
671                 }
672                 
673 #region Interface implementations.
674                 IEnumerator <T> IEnumerable <T>.GetEnumerator ()
675                 {
676                         return GetEnumerator ();
677                 }
678                 
679                 void ICollection.CopyTo (Array array, int arrayIndex)
680                 {
681                         if (array == null)
682                                 throw new ArgumentNullException ("array"); 
683                         if (array.Rank > 1 || array.GetLowerBound (0) != 0)
684                                 throw new ArgumentException ("Array must be zero based and single dimentional", "array");
685                         Array.Copy (_items, 0, array, arrayIndex, _size);
686                 }
687                 
688                 IEnumerator IEnumerable.GetEnumerator ()
689                 {
690                         return GetEnumerator ();
691                 }
692                 
693                 int IList.Add (object item)
694                 {
695                         try {
696                                 Add ((T) item);
697                                 return _size - 1;
698                         } catch (NullReferenceException) {
699                         } catch (InvalidCastException) {
700                         }
701                         throw new ArgumentException ("item");
702                 }
703                 
704                 bool IList.Contains (object item)
705                 {
706                         try {
707                                 return Contains ((T) item);
708                         } catch (NullReferenceException) {
709                         } catch (InvalidCastException) {
710                         }
711                         return false;
712                 }
713                 
714                 int IList.IndexOf (object item)
715                 {
716                         try {
717                                 return IndexOf ((T) item);
718                         } catch (NullReferenceException) {
719                         } catch (InvalidCastException) {
720                         }
721                         return -1;
722                 }
723                 
724                 void IList.Insert (int index, object item)
725                 {
726                         // We need to check this first because, even if the
727                         // item is null or not the correct type, we need to
728                         // return an ArgumentOutOfRange exception if the
729                         // index is out of range
730                         CheckIndex (index);
731                         try {
732                                 Insert (index, (T) item);
733                                 return;
734                         } catch (NullReferenceException) {
735                         } catch (InvalidCastException) {
736                         }
737                         throw new ArgumentException ("item");
738                 }
739                 
740                 void IList.Remove (object item)
741                 {
742                         try {
743                                 Remove ((T) item);
744                                 return;
745                         } catch (NullReferenceException) {
746                         } catch (InvalidCastException) {
747                         }
748                         // Swallow the exception--if we can't cast to the
749                         // correct type then we've already "succeeded" in
750                         // removing the item from the List.
751                 }
752                 
753                 bool ICollection <T>.IsReadOnly {
754                         get { return false; }
755                 }
756                 bool ICollection.IsSynchronized {
757                         get { return false; }
758                 }
759                 
760                 object ICollection.SyncRoot {
761                         get { return this; }
762                 }
763                 bool IList.IsFixedSize {
764                         get { return false; }
765                 }
766                 
767                 bool IList.IsReadOnly {
768                         get { return false; }
769                 }
770                 
771                 object IList.this [int index] {
772                         get { return this [index]; }
773                         set {
774                                 try {
775                                         this [index] = (T) value;
776                                         return;
777                                 } catch (NullReferenceException) {
778                                         // can happen when 'value' is null and T is a valuetype
779                                 } catch (InvalidCastException) {
780                                 }
781                                 throw new ArgumentException ("value");
782                         }
783                 }
784 #endregion
785                                 
786                 [Serializable]
787                 public struct Enumerator : IEnumerator <T>, IDisposable {
788                         readonly List<T> l;
789                         int next;
790                         readonly int ver;
791
792                         T current;
793
794                         internal Enumerator (List <T> l)
795                                 : this ()
796                         {
797                                 this.l = l;
798                                 ver = l._version;
799                         }
800                         
801                         public void Dispose ()
802                         {
803                         }
804
805                         public bool MoveNext ()
806                         {
807                                 var list = l;
808
809                                 if ((uint)next < (uint)list._size && ver == list._version) {
810                                         current = list._items [next++];
811                                         return true;
812                                 }
813
814                                 if (ver != l._version)
815                                         throw new InvalidOperationException ("Collection was modified; enumeration operation may not execute.");
816
817                                 next = -1;
818                                 return false;
819                         }
820
821                         public T Current {
822                                 get { return current; }
823                         }
824                         
825                         void IEnumerator.Reset ()
826                         {
827                                 if (ver != l._version)
828                                         throw new InvalidOperationException ("Collection was modified; enumeration operation may not execute.");
829
830                                 next = 0;
831                                 current = default (T);
832                         }
833                         
834                         object IEnumerator.Current {
835                                 get {
836                                         if (ver != l._version)
837                                                 throw new InvalidOperationException ("Collection was modified; enumeration operation may not execute.");
838
839                                         if (next <= 0)
840                                                 throw new InvalidOperationException ();
841                                         return current;
842                                 }
843                         }
844                 }
845         }
846 }