CharEnumerator.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / clr / src / BCL / System / CharEnumerator.cs / 1 / CharEnumerator.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
/*============================================================
** 
** Class: CharEnumerator 
**
** 
** Purpose: Enumerates the characters on a string.  skips range
**          checks.
**
** 
============================================================*/
namespace System { 
 
    using System.Collections;
    using System.Collections.Generic; 

[System.Runtime.InteropServices.ComVisible(true)]
    [Serializable]
    public sealed class CharEnumerator : IEnumerator, ICloneable, IEnumerator { 
        private String str;
        private int index; 
        private char currentElement; 

        internal CharEnumerator(String str) { 
            this.str = str;
            this.index = -1;
        }
 
        public Object Clone() {
            return MemberwiseClone(); 
        } 

        public bool MoveNext() { 
            if (index < (str.Length-1)) {
                index++;
                currentElement = str[index];
                return true; 
            }
            else 
                index = str.Length; 
            return false;
 
        }

        void IDisposable.Dispose() {
        } 

        ///  
        Object IEnumerator.Current { 
            get {
                if (index == -1) 
                    throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
                if (index >= str.Length)
                    throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));
 
                return currentElement;
            } 
        } 

        public char Current { 
            get {
                if (index == -1)
                    throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
                if (index >= str.Length) 
                    throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));
                return currentElement; 
            } 
        }
 
        public void Reset() {
            currentElement = (char)0;
            index = -1;
        } 
    }
}
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK