public class Product
{
/// <summary>
/// 自增ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 主键
/// </summary>
public string Code { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 类型
/// </summary>
public string Category { get; set; }
/// <summary>
/// 价格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 生产日期
/// </summary>
public DateTime ProduceDate { get; set; }
public override string ToString()
{
return string.Format("{0}{1}{2}{3}{4}{5}", ID.ToString().PadLeft(), Category.PadLeft(), Code.PadLeft(), Name.PadLeft(), Price.ToString().PadLeft(), ProduceDate.ToString("yyyy-M-d").PadLeft());
}
public static ProductCollection GetSampleCollection()
{
ProductCollection collection = new ProductCollection(
new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product1" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product2" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product3" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product4" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product5" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product6" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product7" }
);
return collection;
}
}

产品类

public class ProductCollection : IEnumerable<Product>
{
private List<Product> list = new List<Product>();
private Hashtable table;
public ProductCollection()
{
table = new Hashtable();
}
public ProductCollection(params Product[] array)
{
table = new Hashtable();
foreach (Product item in array)
{
this.Add(item);
}
}
public ICollection Keys
{
get { return table.Keys; }
}
private string getKey(int index)
{
if (index < || index > table.Keys.Count) throw new Exception("索引超出了范围");
string selected = "";
int i = ;
foreach (string key in table.Keys)
{
if (i == index)
{
selected = key; break;
}
i++;
}
return selected;
}
/// <summary>
/// 索引器 支持类似于collection[index]这样的访问
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Product this[int index]
{
get { string key = getKey(index); return table[key] as Product; }
set { string key = getKey(index); table[key] = value; }
}
private string getKey(string key)
{
foreach (string k in table.Keys)
{
if (key == k)
{
return key;
}
}
throw new Exception("不存在此键值");
}
/// <summary>
/// 索引器 类似于collection[key]这样的访问
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Product this[string key]
{
get { string selected = getKey(key); return table[selected] as Product; }
set { string selected = getKey(key); table.Remove(table[selected]); this.Add(value); }
}
/// <summary>
/// 在末尾添加成员
/// </summary>
/// <param name="item"></param>
public void Add(Product item)
{
//确保key不重复
foreach (string key in table.Keys)
{
if (key == item.Code) throw new Exception("产品代码不能重复");
}
table.Add(item.Code, item);
}
/// <summary>
/// 在任意位置添加成员
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, Product item) { }
/// <summary>
/// 移除某一成员
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(Product item)
{
return true;
}
/// <summary>
/// 移除某一位置的成员
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool RemoveAt(int index) { return true; }
/// <summary>
/// 清除所有成员
/// </summary>
public void Clear() { table.Clear(); }
public IEnumerator<Product> GetEnumerator()
{
return new ProductEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new ProductEnumerator(this);
}
/// <summary>
/// 获得集合的成员数量
/// </summary>
public int Count { get { return table.Keys.Count; } }
public class ProductEnumerator : IEnumerator<Product>
{
private ProductCollection collection;
private int index;
public ProductEnumerator(ProductCollection productCollection)
{
this.collection = productCollection;
index = -;
}
public Product Current { get { return collection[index]; } }
object IEnumerator.Current { get { return collection[index]; } }
public void Dispose() { }
public bool MoveNext()
{
index++;
if (index >= collection.Count) return false;
else return true;
}
public void Reset() { index = -; }
}
}

for和foreach都可进行的迭代

ProductCollection pc = Product.GetSampleCollection();
//for (int i = 0; i < pc.Count-1; i++)
//{
// string line = pc[i].ToString();
// Console.WriteLine(line);
//}
foreach (string item in pc.Keys)
{
Console.WriteLine(pc[item]);
}
Console.ReadKey();

测试代码

C#可遍历的集合的更多相关文章

  1. 遍历List集合,删除符合条件的元素

    List集合的遍历有三种方式:增强for循环,普通for循环,Iterator迭代器遍历 如果只是对集合进行遍历,以上三种循环都可正常遍历: (1)增强For循环遍历List集合 List<St ...

  2. 用<forEach>遍历list集合时,提示我找不到对象的属性

    <c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...

  3. freemarker 直接使用List来遍历set集合,可能会报错

    转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/08/528.html freemarker  直接使用List来遍历set集合,可 ...

  4. DOM操作中,遍历动态集合的注意事项。ex: elem.children

    elem.childNodes和elem.children返回的都是动态集合. 动态集合(live collection): 不实际存储元素和属性值 每次访问集合都重新查找DOM树 遍历动态集合:   ...

  5. 遍历Map集合:java.util.Map.Entry、KeySet两种方式

    遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...

  6. 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合

    package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...

  7. Jsp使用遍历List集合

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. (2)集合 遍历set集合

    set集合的一些方法 Set<String> set1=new HashSet<String>(); set1.add("a"); set1.add(&qu ...

  9. (1)集合 ---遍历map集合

    Map接口     实现Map接口的类用来存储键(key)-值(value) 对.Map 接口的实现类有HashMap和TreeMap等.Map类中存储的键-值对通过键来标识,所以键值不能重复. Ha ...

  10. 遍历Map集合的几种方式

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...

随机推荐

  1. Wi-Fi科普讲稿

    Wi-Fi 从入门到?? 组员:deleted 什么是Wi-Fi Wi-Fi 在中文里又称作"无线热点",是Wi-Fi联盟制造商的商标做为产品的品牌认证,是一个创建于IEEE 80 ...

  2. OpenERP 中国财务模块 调整

    最开始的模样是这个样子的 后三行是没用的,于是在RML文件中注释掉相关的代码,改进后的界面如下: 这个样子看起来是好多了,但是 数量跟是十亿千百的那块看起来还是很别扭,调整行高后的结果: 最诡异的事情 ...

  3. Mac下的浏览器类似Windows中Ctrl+F5的不请求缓存刷新页面的快捷键

    正常方式: [shitf]+[command]+[r] 如果改过快捷键的: [fn]+[shift]+[command]+[f]

  4. 高性能web服务器(热死你)Resin Linux的安装、配置、部署,性能远超Nginx支持Java、PHP等

    高性能web服务器(热死你)Resin Linux的安装.配置.部署,性能远超Nginx支持Java.PHP等 一.    安装resin 1.  下载resin: 下载地址:http://cauch ...

  5. 终于解决了贴吧手机版的一个重大BUG

    终于解决了贴吧手机版的一个重大BUG 别诧异虽然同一个域名,但是,PC 和手机打开完全不一样的体验 http://tieba.yunxunmi.com/ 吃点夜校准备做梦去!! 发现 我云贴吧 一个  ...

  6. Javascript之in操作符的用法

    in操作符是js里面常用的一个操作符,下面是其几个常用的功能: 1.配合for语句循环遍历/迭代数组中的元素 2.配合for语句循环遍历/迭代集合中的属性 3.判断对象是否是数组的元素 4.判断对象是 ...

  7. shiro学习笔记_0400_自定义realm实现身份认证

     自定义Realm实现身份认证 先来看下Realm的类继承关系: Realm接口有三个方法,最重要的是第三个方法: a) String getName():返回此realm的名字 b) boolean ...

  8. JavaScript数据结构-15.二叉树

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Maven jdk.1.7

    settings.xml <profile> <id>jdk17</id> <activation>  <activeByDefault>t ...

  10. Java将Excel的列数以字母表示的字符串转换成数字表示

    我们知道,在 Excel 中,行数用数字表示,而列数是用字母表示的(如下图所示),有时候需要把它转换成数字来使用,或者把数字转换成字母.(例如使用POI操作Excel) 下面是转换代码,用来进行字母和 ...