C# 自定义集合
自定义类型
public class Product
{
public int Id { get; set; } // 自增ID
public string Name { get; set; } // 名称
public string Code { get; set; } // 主键
public string Category { get; set; } // 类型
public decimal Price { get; set; } // 价格
public DateTime ProduceDate { get; set; } //产品时间 /// <summary>
/// 重写ToString 方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format("{0}{1}{2}{3}{4}{5}",
this.Id.ToString().PadLeft(2), this.Category.PadLeft(15),
this.Code.PadLeft(7), this.Name.PadLeft(17), this.Price.ToString().PadLeft(8),
this.ProduceDate.ToString("yyyy-M-d").PadLeft(13));
}
public static ProductCollection GetSampleCollection()
{
ProductCollection collection = new ProductCollection(
new Product { Id = 1, Code = "1001", Category = "Red Wine", Name = "Torres Coronas", Price = 285.5m, ProduceDate = DateTime.Parse("1997-12-8") },
new Product { Id = 3, Code = "2001", Category = "White Spirit", Name = "Mao Tai", Price = 1680m, ProduceDate = DateTime.Parse("2001-5-8") },
new Product { Id = 4, Code = "2013", Category = "White Spirit", Name = "Wu Liang Ye", Price = 1260m, ProduceDate = DateTime.Parse("2005-8-1") },
new Product { Id = 8, Code = "3001", Category = "Beer", Name = "TSINGTAO", Price = 6.5m, ProduceDate = DateTime.Parse("2012-4-21") },
new Product { Id = 11, Code = "1003", Category = "Red Wine", Name = "Borie Noaillan", Price = 468m, ProduceDate = DateTime.Parse("1995-7-6") },
new Product { Id = 15, Code = "1007", Category = "Red Wine", Name = "Pinot Noir Rose", Price = 710m, ProduceDate = DateTime.Parse("1988-9-10") },
new Product { Id = 17, Code = "3009", Category = "Beer", Name = "Kingway", Price = 5.5m, ProduceDate = DateTime.Parse("2012-6-13") }
);
return collection;
}
}
自定义的集合方法
public class ProductCollection
{
public 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; } } /// <summary>
/// 根据索引获取当前Key值
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public string GetKeys(int index)
{
if (index < 0 || index > table.Keys.Count)
throw new Exception("超出索引范围");
string selectN = "";
int i = 0;
foreach (string item in table.Keys)
{
if (index == i)
{
selectN = item; break;
}
i++;
}
return selectN;
} public Product this[int index]
{
get
{
string key = GetKeys(index);
return table[key] as Product;
}
set { string key = GetKeys(index); table[key] = value; }
}
/// <summary>
/// 根据Key值获得对应内容
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public string GetKeys(string Name)
{
foreach (string item in table.Keys)
{
if (item==Name)
{
return item;
}
}
throw new Exception("不存在此键值");
} public Product this[string Name]
{
get
{
string selects = GetKeys(Name);
return table[selects] as Product;
}
set
{
string key = GetKeys(Name);
table.Remove(table[key]);
this.Add(value);
}
} /// <summary>
/// 添加功能
/// </summary>
/// <param name="item">添加类</param>
public void Add(Product item)
{
foreach (string key in table.Keys)
{
if(key==item.Code)
throw new Exception("产品代码不能重复");
}
table.Add(item.Code,item);
} /// <summary>
/// 移除功能
/// </summary>
/// <param name="item">添加类</param>
public bool Remove(Product item)
{
try
{
table.Remove(item.Code);
return true;
}
catch
{ return false;
} }
/// <summary>
/// 移除指定索引项
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool Remove(int index)
{ if (index < 0 || index > table.Count)
throw new Exception("超出索引范围");
string key = GetKeys(index);
table.Remove(key);
return true;
}
/// <summary>
/// 清除所有内容
/// </summary>
public void clear()
{
table = new Hashtable();
}
/// <summary>
/// 获取总数
/// </summary>
public int Count { get { return table.Keys.Count; } } }
C# 自定义集合的更多相关文章
- 《C#本质论》读书笔记(16)构建自定义集合
16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...
- 使用yield关键字让自定义集合实现foreach遍历
一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...
- UICollectionView(集合视图)以及自定义集合视图
一.UICollectionView集合视图 其继承自UIScrollView. UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...
- [c#基础]集合foreach的必要条件和自定义集合
引言 最近翻看了之前的学习笔记,看到foreach,记得当时老师讲的时候,有点犯浑,不是很明白,这好比,上小学时,你不会乘法口诀,但是随着时间的增长,你不自觉的都会了,也悟出个小道理,有些东西,你当时 ...
- 集合、拆箱、装箱、自定义集合的foreach
集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...
- C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能
1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...
- C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能
IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...
- 【VBA编程】10.自定义集合
自定义集合类型,类似于变量声明,只是要将Dim关键字和New collection关键字搭配起来使用,其语法描述如下:其中集合名的命名方式同于标准变量的命名 Dim 集合名 As New collec ...
- 实现自定义集合的可枚举类型(IEnumerable)和枚举数(IEnumerator )
下面的代码示例演示如何实现自定义集合的 IEnumerable 和 IEnumerator 接口: using System; using System.Collections; using Syst ...
随机推荐
- mysql 存储结构
mysql存储结构:数据库->表->数据 1)管理数据库 增:create database sjk; 删:drop database sjk; 改:alter database sjk; ...
- Java Web架构知识整理——记一次阿里面试经历
惭愧,从一次电面说起.我个人在某国企做一名软件设计师,国企大家都懂的,待遇一般而且没啥意思,做的方向基本都是操作系统.驱动和工具软件的开发,语言基本都是C/C++.最近也想跳槽,刚好有幸得到了一次阿里 ...
- QTP 场景恢复– 函数调用
创建自动化测试是为了实现无人值守下运行,但也给开发人员带来一些问题.假如你离开办公室前启动测试,想要让它通宵运行.然而,由于不可预见的错误,您的测试会在某一点停止,中断了测试结果.因此QTP中引入场景 ...
- Botposter.com集群ETCD2.3.7升级至3.0实录[原创]
7月1日,为庆祝我党生日,ETCD隆重发布了3.0版本.Botposter.com也在第一时间对集群进行了升级.本文是升级过程的记录与总结(文中假设读者已经使用或测试过ETCD V2,如有不妥请见谅) ...
- yii学习小结
对yii框架搭建的平台运维过程中,会不断地发现很多新的特性和问题,现一一记录下来,便于后续学习~ 1.日志 在/runtime目录中 参考:http://www.cnblogs.com/you ...
- 洛谷P2085 最小函数值(minval)
P2085 最小函数值(minval) 218通过 487提交 题目提供者该用户不存在 标签堆高级数据结构 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 有n个函数, ...
- SQL Server 索引和视图
Ø 索引 1. 什么是索引 索引就是数据表中数据和相应的存储位置的列表,利用索引可以提高在表或视图中的查找数据的速度. 2. 索引分类 数据库中索引主要分为两类:聚集索引和非聚集索引.SQL Serv ...
- Android IOS WebRTC 音视频开发总结(三五)-- chatroulette介绍
本文主要从技术角度介绍chatroulette,文章来自博客园RTC.Blacker,支持原创,转载请说明出处. 很多人不知道或没用过chatroulette,下面先来张界面截图让大家有个整体了解: ...
- vsftp快速配置
/etc/vsftpd/vsftpd.confanonymous_enable=YESlocal_enable=YESwrite_enable=YESlocal_umask=022dirmessage ...
- C++ STL 简单记录
1,STL提供三种类型的组件:容器.迭代器.算法. 容器: 顺序容器(vector.list.deque.string等)是一系列元素的有序集合: 关联容器(set.multiset.map.mult ...