C#可遍历的集合
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#可遍历的集合的更多相关文章
- 遍历List集合,删除符合条件的元素
List集合的遍历有三种方式:增强for循环,普通for循环,Iterator迭代器遍历 如果只是对集合进行遍历,以上三种循环都可正常遍历: (1)增强For循环遍历List集合 List<St ...
- 用<forEach>遍历list集合时,提示我找不到对象的属性
<c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...
- freemarker 直接使用List来遍历set集合,可能会报错
转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/08/528.html freemarker 直接使用List来遍历set集合,可 ...
- DOM操作中,遍历动态集合的注意事项。ex: elem.children
elem.childNodes和elem.children返回的都是动态集合. 动态集合(live collection): 不实际存储元素和属性值 每次访问集合都重新查找DOM树 遍历动态集合: ...
- 遍历Map集合:java.util.Map.Entry、KeySet两种方式
遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合
package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...
- Jsp使用遍历List集合
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- (2)集合 遍历set集合
set集合的一些方法 Set<String> set1=new HashSet<String>(); set1.add("a"); set1.add(&qu ...
- (1)集合 ---遍历map集合
Map接口 实现Map接口的类用来存储键(key)-值(value) 对.Map 接口的实现类有HashMap和TreeMap等.Map类中存储的键-值对通过键来标识,所以键值不能重复. Ha ...
- 遍历Map集合的几种方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...
随机推荐
- #PHP# 华为云 API 方式发送短信
使用给华为云 消息 服务 API 方式发送短信 代码来自华为云,已通过测试 <?php /** * 华为云发送短信示例代码 * 本段代码需要使用自己的配置信息才能正常运行,出配置信息外,不需要改 ...
- 2.CentOS6.5下的DNS主从区域传送配置
接着<1.CentOS6.5下的基础DNS配置>来说,主从区域传送只能让从服务器来进行传送,不给任何人传送,我们看看上一章节<1.CentOS6.5下的基础DNS配置>是否可传 ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...
- Mac 10.12安装OpenVPN客户端
说明: 1.在Mac下有很多漂亮的客户端可以安装,比如Tunnelblick这些等等. 2.但这里直接先原版的OpenVPN进行搭建,这个比较爽. 安装: brew install openvpn 提 ...
- 使用VNC访问Windows桌面
1. 背景介绍 两台电脑,一个笔记本,一个台式机 笔记本上装的是Windows 10, 通过上网小助手上网 (P.S. 上网小助手...Stupid Policy...) 台式机上装的是Ubuntu ...
- varchar(n) 和 int(n)
mysql(测试版本为5.6) 的varchar必须加范围,否则报错: mysql> CREATE TABLE test ( -> id INT, -> name VARCHAR - ...
- 手动添加Git Bash到右键菜单
1. 打开注册表. 2. 找到[HKEY_CLASSES_ROOT\Directory\Background]. 3. 在[Background]下如果没有[shell],则右键-新建项[shell] ...
- HTML5学习笔记:HTML5基于本地存储SQLite的每日工作任务清单程序.[只支持chrome]
使用环境:Chrome 36.0...+ 技术:HTML5 目的:习练HTML5 功能概述:记录管理每天工作内容,便签清单 HTML5+CSS3呈现UI,JavaScript操作数据库,SQLite存 ...
- Bitcoin交易及验证
目录 UTXO 理解 交易的结构 交易的确认 交易验证 逆波兰表示法 使用逆波兰表示法验证交易 UTXO 理解 未花费交易输出: Unspent Transxtion output UTXO---用比 ...
- [PY3]——时间处理——datetime | calendar
Python3的日期/时间处理模块 datetime的格式化符号 格式化符号 表示 %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 日 ...