zoukankan      html  css  js  c++  java
  • C# 2.0学习之集合2

    编译与执行:
    csc tokens2.cs tokens2

    tokens2.cs :

    //Copyright (C) Microsoft Corporation.  All rights reserved.

    // tokens2.cs
    using System;
    using System.Collections;

    public class Tokens: IEnumerable
    {
       private string[] elements;

       Tokens(string source, char[] delimiters)
       {
          elements = source.Split(delimiters);
       }

       // IEnumerable Interface Implementation:

       public TokenEnumerator GetEnumerator() // non-IEnumerable version
       {
          return new TokenEnumerator(this);
       }

       IEnumerator IEnumerable.GetEnumerator() // IEnumerable version
       {
          return (IEnumerator) new TokenEnumerator(this);
       }

       // Inner class implements IEnumerator interface:

       public class TokenEnumerator: IEnumerator
       {
          private int position = -1;
          private Tokens t;

          public TokenEnumerator(Tokens t)
          {
             this.t = t;
          }

          public bool MoveNext()
          {
             if (position < t.elements.Length - 1)
             {
                position++;
                return true;
             }
             else
             {
                return false;
             }
          }

          public void Reset()
          {
             position = -1;
          }

          public string Current // non-IEnumerator version: type-safe
          {
             get
             {
                return t.elements[position];
             }
          }

          object IEnumerator.Current // IEnumerator version: returns object
          {
             get
             {
                return t.elements[position];
             }
          }
       }

       // Test Tokens, TokenEnumerator

       static void Main()
       {
          Tokens f = new Tokens("This is a well-done program.",
             new char [] {' ','-'});
          foreach (string item in f) // try changing string to int
          {
             Console.WriteLine(item);
          }
       }
    }

  • 相关阅读:
    2019.1.10英语笔记
    2019.1.9专业课笔记
    团队触发器
    团队脚本备份
    导表
    oslo.config
    nginx启动、重启、关闭
    常见的awk内建变量
    LVM
    Django, one-to-many, many-to-many
  • 原文地址:https://www.cnblogs.com/llbofchina/p/434162.html
Copyright © 2011-2022 走看看