C#将类实现集合
来源:互联网 发布:音效软件手机版 编辑:程序博客网 时间:2024/05/21 12:44
using System;
using System.Collections;
/**//// <summary>
/// 实现集合的类Name
/// </summary>
public class Name : IEnumerable
...{
private int _size;
private string[] _names;
public Name(int size)
...{
_size = size;
_names = new string[_size];
}
public Name() ...{ }
public int Length
...{
get ...{ return _size; }
set
...{
_size = value;
_names = new string[_size];
}
}
public string this[int index]
...{
get
...{
if (index < 0 || index >= _size)
...{
throw new InvalidOperationException("下标越界!");
}
else
...{
return _names[index];
}
}
set
...{
if (index < 0 || index >= _size)
...{
throw new InvalidOperationException("下标越界!");
}
else
...{
_names[index]=value;
}
}
}
public IEnumerator GetEnumerator()
...{
return new NameEnumerator(this,_size);
}
private class NameEnumerator : IEnumerator
...{
Name myName;
int location;
int _size;
public NameEnumerator(Name theName,int size)
...{
_size = size;
this.myName = theName;
location = -1;
}
public bool MoveNext()
...{
location++;
return (location >= _size) ? false : true;
}
public object Current
...{
get
...{
if (location < 0 || location >= _size )
...{
throw new InvalidOperationException("越界错误!");
}
else
...{
return myName[(int)location];
}
}
}
public void Reset()
...{
location = -1;
}
}
}
using System.Collections;
/**//// <summary>
/// 实现集合的类Name
/// </summary>
public class Name : IEnumerable
...{
private int _size;
private string[] _names;
public Name(int size)
...{
_size = size;
_names = new string[_size];
}
public Name() ...{ }
public int Length
...{
get ...{ return _size; }
set
...{
_size = value;
_names = new string[_size];
}
}
public string this[int index]
...{
get
...{
if (index < 0 || index >= _size)
...{
throw new InvalidOperationException("下标越界!");
}
else
...{
return _names[index];
}
}
set
...{
if (index < 0 || index >= _size)
...{
throw new InvalidOperationException("下标越界!");
}
else
...{
_names[index]=value;
}
}
}
public IEnumerator GetEnumerator()
...{
return new NameEnumerator(this,_size);
}
private class NameEnumerator : IEnumerator
...{
Name myName;
int location;
int _size;
public NameEnumerator(Name theName,int size)
...{
_size = size;
this.myName = theName;
location = -1;
}
public bool MoveNext()
...{
location++;
return (location >= _size) ? false : true;
}
public object Current
...{
get
...{
if (location < 0 || location >= _size )
...{
throw new InvalidOperationException("越界错误!");
}
else
...{
return myName[(int)location];
}
}
}
public void Reset()
...{
location = -1;
}
}
}
-------------------------------------------------------------------------------------------------------------------------
测试:
Name myName = new Name(3);
myName[0] = "Pzh";
myName[1] = "Youniao";
myName[2] = "Godling";
foreach (string name in myName)
{
Response.Write("我的名字:"+name+"<br />");
}
-------------------------------------------------------------------------------------------------------------------------
输出:
我的名字:Pzh
我的名字:Youniao
我的名字:Godling
给类加了属性Length 可以改变‘容量’,不过数组被重新初始化了,付值前的字符串会丢失,这里随便写个属性表示这么个意思,如果要实现数组列表ArrayList的功能有兴趣的再改进
- C#将类实现集合
- C#将集合类转换成DataTable
- 【C# 工具类】将DataTable转化为 List集合/对象
- c# 将实体类集合转化为datetable格式案例
- 将List集合插入到数据库中,C#中的事物实现
- 多映射通用集合类(C#实现)--支持一键多值存储
- 多映射通用集合类(C#实现)--支持一键多值存储
- C# 实现Sort接口 排序自定义实体类集合
- C#将Json字符串反序列化成List对象类集合
- C#集合类
- C# 集合类
- C#集合类选择
- C#集合类
- C#中的集合类
- C#集合类概述
- c#集合类
- C#集合类
- C# 集合类
- 推荐一个有用的工具--Aargo
- oracle 中如何查看有那个表被锁了?
- 魔兽局域网协议之TCP部份分析-原创 一 待续
- 安装VSS时,Um.dat may be corrupt
- 《软件估算》——你的估算水平如何
- C#将类实现集合
- VSS添加用户时出错--File .../Users Not Found
- 微软 BizTalk Server 2006 R2 定价和许可
- 排序 自己选择是从小到小还是从小到大排序。
- C#中的抽象类和接口
- 微软公司 BizTalk Server 2006 R2 产品价格与许可常见问题
- 从emacs开始
- Maemo Linux手机平台系列分析:9 Maemo平台开发之 使用D-Bus信号做异步操作
- ajax+ajax futures+asp.net 2.0bug cannot create page method