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

     编译与执行:
    csc tokens.cs
                tokens



    tokens.cs :


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

    // tokens.cs
    using System;
    // The System.Collections namespace is made available:
    using System.Collections;

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

       Tokens(string source, char[] delimiters)
       {
          // Parse the string into tokens:
          elements = source.Split(delimiters);
       }

       // IEnumerable Interface Implementation:
       //   Declaration of the GetEnumerator() method
       //   required by IEnumerable
       public IEnumerator GetEnumerator()
       {
          return new TokenEnumerator(this);
       }

       // Inner class implements IEnumerator interface:
       private class TokenEnumerator : IEnumerator
       {
          private int position = -1;
          private Tokens t;

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

          // Declare the MoveNext method required by IEnumerator:
          public bool MoveNext()
          {
             if (position < t.elements.Length - 1)
             {
                position++;
                return true;
             }
             else
             {
                return false;
             }
          }

          // Declare the Reset method required by IEnumerator:
          public void Reset()
          {
             position = -1;
          }

          // Declare the Current property required by IEnumerator:
          public object Current
          {
             get
             {
                return t.elements[position];
             }
          }
       }

       // Test Tokens, TokenEnumerator

       static void Main()
       {
          // Testing Tokens by breaking the string into tokens:
          Tokens f = new Tokens("This is a well-done program.",
             new char[] {' ','-'});
          foreach (string item in f)
          {
             Console.WriteLine(item);
          }
       }
    }

  • 相关阅读:
    QuickStart系列:docker部署之Gitlab本地代码仓库
    https环境搭建(本地搭建)
    docker搭建elk
    使用本机IP调试web项目
    VC++ 异常处理 __try __except的用法
    Delphi编程常用快捷键大全
    Delphi2007安装报Invalid Serial Number问题
    Cannot create file "C:UsersADMINI~1AppDataLocalTempEditorLineEnds.ttr"
    delphi 调试的时候变量全部显示Inaccessible value的解决办法
    Delphi idhttp解决获取UTF-8网页中文乱码问题
  • 原文地址:https://www.cnblogs.com/llbofchina/p/434152.html
Copyright © 2011-2022 走看看