例一
//Dictionary
System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key1","value1");
//ArrayList
System.Collections.ArrayList list=new System.Collections.ArrayList();
list.Add(1);
list.Add(2);
for(int i=0;i<list.Count;i++)
{
System.Console.WriteLine(list[i]);
}
//HashTable
System.Collections.Hashtable table=new System.Collections.Hashtable();
table.Add("table1",1);
table.Add("table2",2);
System.Collections.IDictionaryEnumerator d=table.GetEnumerator();
while(d.MoveNext())
{
System.Console.WriteLine(d.Entry.Key);
}
//Queue
System.Collections.Queue queue=new System.Collections.Queue();
queue.Enqueue(1);
queue.Enqueue(2);
System.Console.WriteLine(queue.Peek());
while(queue.Count>0)
{
System.Console.WriteLine(queue.Dequeue());
}
//SortedList
System.Collections.SortedList list=new System.Collections.SortedList();
list.Add("key2",2);
list.Add("key1",1);
for(int i=0;i<list.Count;i++)
{
System.Console.WriteLine(list.GetKey(i));
}
//Stack
System.Collections.Stack stack=new System.Collections.Stack();
stack.Push(1);
stack.Push(2);
System.Console.WriteLine(stack.Peek());
while(stack.Count>0)
{
System.Console.WriteLine(stack.Pop());
}
例二
using System;
using System.Collections;
namespace myCon
{
class Student
{
public Student(){}
public Student(string strName)
private string name;
public string Name
{
get{ return name; }
}
public string ToString()
{
return name;
}
}
class Connect: IEnumerable
{
IEnum ie = new IEnum();
public void Add(object obj)
{
ie.lst.Add(obj);
}
public void ReMove(object obj)
{
ie.lst.Remove(obj);
}
public IEnumerator GetEnumerator()
{
return ie;
}
class IEnum: IEnumerator
{
public int idx = -1;
public ArrayList lst = new ArrayList();
public void Reset()
{
idx = -1;
}
public object Current
{
get
{
if (idx>=0 && idx<lst.Count)
return lst[idx];
return null;
}
}
public bool MoveNext()
{
idx++;
return idx<lst.Count;
}
}
}
class Class1
{
static void Main(string[] args)
{
Connect con = new Connect();
con.Add(new Student("aaa"));
con.Add(new Student("bbb"));
con.Add(new Student("ccc"));
foreach(Student stu in con)
{
Console.WriteLine(stu.Name);
}
}
}
}