漠漠星的专栏

  有水、有草、有星星,日月星辰,天下之大;以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!...

C# 2.0学习之--集合

 编译与执行:
csc tokens.cs
tokens

 

// 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);
      }
   }
}



tokens.cs :


//Copyright (C) Microsoft Corporation.  All rights reserved.
阅读更多
个人分类: 软件开发 ASP .NET
上一篇C# 2.0学习之--数组
下一篇C# 2.0学习之--集合2
想对作者说点什么? 我来说一句

ASP.NET 2.0 & C# 2.0学习资料

2008年03月14日 7.2MB 下载

没有更多推荐了,返回首页

关闭
关闭