1.HashTable

private static void HashTableSample()
{
// Creates and initializes a new Hashtable.
Hashtable table = new Hashtable();
table.Add("1","frist");
table.Add("2","second");
table.Add("3","third");
// Displays the properties and values of the Hashtable.
Console.WriteLine( "table" );
Console.WriteLine( " Count: {0}", table.Count );
Console.WriteLine( " Keys and Values:" );
//how to print out its keys and values
foreach(DictionaryEntry d in table)
{
Console.WriteLine ("{0}\t{1}", d.Key, d.Value);
}
Console.ReadLine();
}
2.ArrayList

private static void ArrayListSample()
{
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( "\tCount: {0}", myAL.Count );
Console.WriteLine( "\tCapacity: {0}", myAL.Capacity );
Console.Write( "\tValues:" );
foreach(object o in myAL)
{
Console.WriteLine(o.ToString());
}
Console.ReadLine();
}
3.Queue

private static void QueueSmaple()
{
// Creates and initializes a new Queue.
Queue myQ = new Queue();
myQ.Enqueue("Hello");
myQ.Enqueue("World");
myQ.Enqueue("!");
// Displays the properties and values of the Queue.
Console.WriteLine( "myQ" );
Console.WriteLine( "\tCount: {0}", myQ.Count );
Console.Write( "\tValues:" );
System.Collections.IEnumerator myEnumerator = myQ.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
Console.ReadLine();
}
4.自定义集合类:

public class Employee
{
private int _id;
private string _name;
public Employee(int id,string name)
{
_id = id;
_name = name;
}
public int id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
public class EmployeeCollection : ICollection
{
private int count;
ArrayList mylist = new ArrayList();
#region 实现接口ICollection
public virtual int Count
{
get
{
return count;
}
}
public virtual bool IsSynchronized
{
get
{
return false;
}
}
public virtual Object SyncRoot
{
get
{
return this;
}
}
public virtual void CopyTo(Array array,int index)
{
//do nothing
}
public virtual IEnumerator GetEnumerator()
{
return null;
}
#endregion
public virtual Employee this[int i]
{
get
{
return (Employee)this.mylist[i] ;
}
}
public void AddEmployee(int id,string name)
{
Employee emp = new Employee(id,name);
mylist.Add(emp);
count = mylist.Count;
}
public Employee GetEmployee(int id)
{
foreach(Employee e in mylist)
{
if(id == e.id)
{
return e;
}
else
{
return null;
}
}
return null;
}
}
遍历自定义集合类:

EmployeeCollection eCollection = new EmployeeCollection();
eCollection.AddEmployee(1,"Engine");
eCollection.AddEmployee(2,"Water");
eCollection.AddEmployee(3,"Arvin");
eCollection.AddEmployee(4,"Vikki");
eCollection.AddEmployee(5,"Gray");
eCollection.AddEmployee(6,"Tom");
for(int i = 0; i <eCollection.Count;i++)
{
Console.WriteLine(eCollection[i].name);
}
Console.ReadLine();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)