C#泛型集合 using System.Collections.Generic
来源:互联网 发布:数据库高级培训 编辑:程序博客网 时间:2024/05/18 02:10
泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。
很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:
非泛型集合类泛型集合类ArrayListListHashTableDIctionaryQueueQueueStackStackSortedListSortedList
很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:
非泛型集合类泛型集合类ArrayListListHashTableDIctionaryQueueQueueStackStackSortedListSortedList
为什么要用泛型集合?
在C# 2.0之前,主要可以通过两种方式实现集合:
a.使用ArrayList
直接将对象放入ArrayList,操作直观,但由于集合中的项是Object类型,因此每次使用都必须进行繁琐的类型转换。
b.使用自定义集合类
比较常见的做法是从CollectionBase抽象类继承一个自定义类,通过对IList对象进行封装实现强类型集合。这种方式要求为每种集合类型写一个相应的自定义类,工作量较大。泛型集合的出现较好的解决了上述问题,只需一行代码便能创建指定类型的集合。
什么是泛型?
泛型是C# 2.0中的新增元素(C++中称为模板),主要用于解决一系列类似的问题。这种机制允许将类名作为参数传递给泛型类型,并生成相应的对象。将泛型(包括类、接口、方法、委托等)看作模板可能更好理解,模板中的变体部分将被作为参数传进来的类名称所代替,从而得到一个新的类型定义。
怎样创建泛型集合?
主要利用System.Collections.Generic命名空间下面的List<T>泛型类创建集合,语法如下:
List<T> ListOfT = new List<T>();
其中的"T"就是所要使用的类型,既可以是简单类型,如string、int,也可以是用户自定义类型。下面看一个具体例子。
定义Person类如下:
class Person
{
private string _name; //姓名
private int _age; //年龄
//创建Person对象
public Person(string Name,int Age)
{
this._name= Name;
this._age = Age;
}
//姓名
public string Name
{
get {return _name; }
}
//年龄
public int Age
{
get {return _age; }
}
}
//创建Person对象
Person p1= new Person("张三",30);
Person p2= new Person("李四",20);
Person p3= new Person("王五",50);
//创建类型为Person的对象集合
List<Person> persons = new List<Person>();
//将Person对象放入集合
persons.Add(p1);
persons.Add(p2);
persons.Add(p3);
//输出第2个人的姓名
Console.Write(persons[1].Name);
{
private string _name; //姓名
private int _age; //年龄
//创建Person对象
public Person(string Name,int Age)
{
this._name= Name;
this._age = Age;
}
//姓名
public string Name
{
get {return _name; }
}
//年龄
public int Age
{
get {return _age; }
}
}
//创建Person对象
Person p1= new Person("张三",30);
Person p2= new Person("李四",20);
Person p3= new Person("王五",50);
//创建类型为Person的对象集合
List<Person> persons = new List<Person>();
//将Person对象放入集合
persons.Add(p1);
persons.Add(p2);
persons.Add(p3);
//输出第2个人的姓名
Console.Write(persons[1].Name);
可以看到,泛型集合大大简化了集合的实现代码,通过它,可以轻松创建指定类型的集合。非但如此,泛型集合还提供了更加强大的功能,下面看看其中的排序及搜索。
- C#泛型集合 using System.Collections.Generic
- C#泛型集合 using System.Collections.Generic
- C#泛型集合 using System.Collections.Generic
- 定义泛型集合的命名空间:System.Collections.Generic
- 为什么要使用using System.Collections.Generic;?
- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin
- C#学习之System.Collections.Generic 与 System.Collections 记录
- System.Collections.Generic
- System.Collections、System.Collections.Generic、System.Collections.Specialized
- System.Collections.Generic 命名空间
- System.Collections.Generic命名空间
- System.Collections.Generic 命名空间
- System.Collections.Generic 命名空间
- 根据给出的值、判断是否构成三角形及类型using System; using System.Collections.Generic; using System.Linq; using System.T
- 使用泛型类型System.Collections.Generic.Icomparer <T> 需要一个类型参数
- MSDN:使用泛型 类型“System.Collections.Generic.IComparer <T> ”需要“1”个类型参数”。
- 使用泛型 类型System.Collections.Generic.KeyValuePair需要2个类型参数
- System.Collections.Generic 命名空间1
- js实现在线测试8代码
- RGB颜色查询对照表
- 一些需要的link
- string 与 char
- 模式识别概述
- C#泛型集合 using System.Collections.Generic
- Android中的Handler总结
- 正则表达式全解
- 普林斯顿大学算法第一周个人总结1
- FLASh RAM
- HDU 1690 最短路
- JAVA 删除文本文件中的空行
- strcpy函数的实现
- 用C++编写COM组件