The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ ArraySubsetEnumerator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ArraySubsetEnumerator.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.ComponentModel
  16. {
  17.    
  18.     using System;
  19.     using System.Collections;
  20.     using System.Diagnostics;
  21.     using System.Security.Permissions;
  22.    
  23.     [HostProtection(SharedState = true)]
  24.     internal class ArraySubsetEnumerator : IEnumerator
  25.     {
  26.        
  27.         private Array array;
  28.         private int total;
  29.         private int current;
  30.        
  31.         public ArraySubsetEnumerator(Array array, int count)
  32.         {
  33.             Debug.Assert(count == 0 || array != null, "if array is null, count should be 0");
  34.             Debug.Assert(array == null || count <= array.Length, "Trying to enumerate more than the array contains");
  35.             this.array = array;
  36.             this.total = count;
  37.             current = -1;
  38.         }
  39.        
  40.         public bool MoveNext()
  41.         {
  42.             if (current < total - 1) {
  43.                 current++;
  44.                 return true;
  45.             }
  46.             else {
  47.                 return false;
  48.             }
  49.         }
  50.        
  51.         public void Reset()
  52.         {
  53.             current = -1;
  54.         }
  55.        
  56.         public object Current {
  57.             get {
  58.                 if (current == -1) {
  59.                     throw new InvalidOperationException();
  60.                 }
  61.                 else {
  62.                     return array.GetValue(current);
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }

Developer Fusion