泛型:通过泛型可以定义类型安全的数据类型,它的最显著应用就是创建集合类,可以约束集合类内的元素类型。
比较典型的泛型集合是List<T>和Dictionary<k,v>。
一、泛型集合List<T>:
示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
private void btnMyClassTest_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
List<Student> students = new List<Student>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Student scofield = new Student("Scofield", Genders.Male, 28, "越狱狱");
Student zhang = new Student("张靓靓", Genders.Female, 20, "唱歌歌");
Student jay = new Student("周杰杰", Genders.Male, 21, "耍双节棍棍");
Teacher jacky = new Teacher("成龙龙", 4);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
添加元素 演示#region 添加元素 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
students.Add(scofield);
students.Add(zhang);
students.Add(jay);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//打印集合数目
MessageBox.Show(string.Format("共包括 {0} 个成员。",
students.Count.ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
存取单个元素 演示#region 存取单个元素 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Student stu1 = students[2];
stu1.SayHi();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
元素遍历 演示#region 元素遍历 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Student stu in students)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(stu.Name);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
删除元素 演示#region 删除元素 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//打印集合数目
MessageBox.Show(string.Format("共包括 {0} 个成员。",
students.Count.ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
students.RemoveAt(0);
//students.Remove(zhang); //通过对象名删除元素
//students.Clear(); 清除所有元素
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//打印集合数目
MessageBox.Show(string.Format("共包括 {0} 个成员。",
students.Count.ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Student stu in students)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(stu.Name);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
二、泛型集合Dictionary<K,V>:编译时检查类型约束,获取元素时无须类型转换,通过Key/Value键/值对保存元素的。
<K,V>中的K表示集合中Key的类型,V表示Value的类型。
示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
private void btnMyClassTest_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Dictionary<String, Student> students = new Dictionary<string, Student>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Student scofield = new Student("Scofield", Genders.Male, 28, "越狱狱");
Student zhang = new Student("张靓靓", Genders.Female, 20, "唱歌歌");
Student jay = new Student("周杰杰", Genders.Male, 21, "耍双节棍棍");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
添加元素 演示#region 添加元素 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
students.Add(scofield.Name, scofield);
students.Add(zhang.Name, zhang);
students.Add(jay.Name, jay);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//打印集合数目
MessageBox.Show(string.Format("共包括 {0} 个学员。",
students.Count.ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
存取单个元素 演示#region 存取单个元素 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Student stu2 = students["周杰杰"];
stu2.SayHi();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
元素遍历 演示#region 元素遍历 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Student student in students.Values)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(student.Name);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
删除元素 演示#region 删除元素 演示
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//打印集合数目
MessageBox.Show(string.Format("共包括 {0} 个学员。",
students.Count.ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
students.Remove("周杰杰");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//打印集合数目
MessageBox.Show(string.Format("共包括 {0} 个学员。",
students.Count.ToString()));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
注意:引入命名空间System.Collections.Generic;
泛型接口也比较重要
小结:数据批量处理方式 :
A、数组容量相对固定(指最大最小不超过两倍),用下标来取元素,
不要求其他更有意义的下标来取数据。
B、List<T>容量可变,可排序。
C、Dictionary<K,V>允许有意义的数据类型作下标。