大家好,我是sunwen.今天下午得知,晚上要去当"更夫".呵呵,我们这个学校很是奇怪,要叫我们轮流去通宵巡逻,从晚上10:30到早上6:00.我有一个物理系的朋友,上次在田家炳楼门前找了一个宣传板,垫在下面就睡了一觉,听了笑死我了!哈哈!
现在我想说的是c#中的容器.这是一个非常重要的话题,因为不管你写什么样的程序,你都不能不与容器打交道.什么是容器呢(倒!).容器就是可以容纳东西的东西(再倒!),在c#和java这种面向对象的编程语言中,容器就被称为可以容纳对象的东东,不是说"一切都是对象吗?"以前,我一个搞c++的程序员朋友告诉我,java中的容器太好用了,比c++好用多了.而作为java的后来者的c#毫无疑问,它的容器功能肯定也是很强大的.
foreach语句是遍历容器的元素的最简单的方法.我们可以用system.collections.ienumerator类和system.collections.ienumerable接口来使用c#中的容器,下面有一个例子,功能是字符串分割器.
000: // collectionclasses\tokens.cs
001: using system;
002: using system.collections;
003:
004: public class tokens : ienumerable
005: {
006: private string[] elements;
007:
008: tokens(string source, char[] delimiters)
009: {
010: elements = source.split(delimiters);
011: }
012:
013: //引用ienumerable接口014:
015: public ienumerator getenumerator()
016: {
017: return new tokenenumerator(this);
018: }
019:
020:
021:
022: private class tokenenumerator : ienumerator
023: {
024: private int position = -1;
025: private tokens t;
026:
027: public tokenenumerator(tokens t)
028: {
029: this.t = t;
030: }
031:
032: public bool movenext()
033: {
034: if (position < t.elements.length - 1)
035: {
036: position++;
037: return true;
038: }
039: else
040: {
041: return false;
042: }
043: }
044:
045: public void reset()
046: {
047: position = -1;
048: }
049:
050: public object current
051: {
052: get
053: {
054: return t.elements[position];
055: }
056: }
057: }
058:
059: // 测试060:
061: static void main()
062: {
063: tokens f = new tokens("this is a well-done program.", new char[] {' ','-'});
064: foreach (string item in f)
065: {
066: console.writeline(item);
067: }
068: }
069: }
这个例子的输出是:
this
is
a
well
done
program.
好了,这一节就说到这了.现在环境不太好,旁边一大帮同学在看vcd,不好搞.
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器