关于object类型:
1.object类型可以来引用任何类型的实例;
2.object类型可以存储任何类型的值;
3.可以定义object类型的参数;
4.可以把object作为返回类型。
但是--这样做有很大的问题
1.会因为程序员没有记住使用的类型而出错,造成类型不兼容;
2.值类型和引用类型的互化即装箱拆箱使系统性能下降。
C#2.0提出的泛型就是避免强制类型转换,减少装箱拆箱提高性能,减少错误。
System.Collections.Generic命名空间提供许多集合类和接口的泛型版本。
定义:
public class GenericList<T>
{
public void Add(T input)//T制定成类型参数
public T Add()//T制定成返回值
}
<T>的T是类型参数,起占位符的作用,编译时被真正类型取代。
使用泛型:
GenericList<int> list1 = new GenericList<int>();
GenericList<string> list2 = new GenericList<string>();
GenericList<类名> list3 = new GenericList<类名>();
GenericList<类名<int>> list4= new GenericList<类名<int>>();
以list1为例编译器生成以下的方法:
public void Add(int input)
public int Add()
有多个类型参数的泛型类:
public class 类名<T,U>
泛型约束:
确保泛型类使用的参数是提供特定方法的类型。
public class GenericList<T> where T : IEmployee
假如IEmployee接口包含A方法,编译器会验证用于替换T的类型一定要实现IEmployee接口。
泛型方法:允许采取定义泛型类时采用的方式
//定义泛型方法
static void Swap<T>(ref T lhs, ref T rhs)
{ T temp; temp = lhs; lhs = rhs; rhs = temp; }
//使用泛型方法
public static void TestSwap(){ int a=1,b=3;
Swap<int>(ref a,ref b);
string s1="Hello",s2="world";
Swap<string>(ref s1,ref s2);}
基础泛型类或接口:
1.object类型可以来引用任何类型的实例;
2.object类型可以存储任何类型的值;
3.可以定义object类型的参数;
4.可以把object作为返回类型。
但是--这样做有很大的问题
1.会因为程序员没有记住使用的类型而出错,造成类型不兼容;
2.值类型和引用类型的互化即装箱拆箱使系统性能下降。
C#2.0提出的泛型就是避免强制类型转换,减少装箱拆箱提高性能,减少错误。
System.Collections.Generic命名空间提供许多集合类和接口的泛型版本。
定义:
public class GenericList<T>
{
public void Add(T input)//T制定成类型参数
public T Add()//T制定成返回值
}
<T>的T是类型参数,起占位符的作用,编译时被真正类型取代。
使用泛型:
GenericList<int> list1 = new GenericList<int>();
GenericList<string> list2 = new GenericList<string>();
GenericList<类名> list3 = new GenericList<类名>();
GenericList<类名<int>> list4= new GenericList<类名<int>>();
以list1为例编译器生成以下的方法:
public void Add(int input)
public int Add()
有多个类型参数的泛型类:
public class 类名<T,U>
泛型约束:
确保泛型类使用的参数是提供特定方法的类型。
public class GenericList<T> where T : IEmployee
假如IEmployee接口包含A方法,编译器会验证用于替换T的类型一定要实现IEmployee接口。
泛型方法:允许采取定义泛型类时采用的方式
//定义泛型方法
static void Swap<T>(ref T lhs, ref T rhs)
{ T temp; temp = lhs; lhs = rhs; rhs = temp; }
//使用泛型方法
public static void TestSwap(){ int a=1,b=3;
Swap<int>(ref a,ref b);
string s1="Hello",s2="world";
Swap<string>(ref s1,ref s2);}
下表列出了五类约束:
约束
|
描述
|
where T: struct
|
类型参数必须为值类型。
|
where T : class
|
类型参数必须为类型。
|
where T : new()
|
类型参数必须有一个公有、无参的构造函数。当于其它约束联合使用时,new()约束必须放在最后。
|
where T : <base class name>
|
类型参数必须是指定的基类型或是派生自指定的基类型。
|
where T : <interface name>
|
类型参数必须是指定的接口或是指定接口的实现。可以指定多个接口约束。接口约束也可以是泛型的。
|
基础泛型类或接口:
泛型类或接口
|
描述
|
对应的非泛型类型
|
Collection<T>
ICollection<T>
|
为泛型容器提供基类
|
CollectionBase
ICollection
|
Comparer<T>
IComparer<T>
IComparable<T>
|
比较两个相同泛型类型的对象是否相等、可排序。
|
Comparer
IComparer
IComparable
|
Dictionary<K, V>
IDictionary<K,V>
|
表示用键组织的键/值对集合。
|
Hashtable
IDictionary
|
Dictionary<K, V>.KeyCollection
|
表示Dictionary<K, V>中键的集合。
|
None.
|
Dictionary<K, V>.ValueCollection
|
表示Dictionary<K, V>中值的集合。
|
None.
|
IEnumerable<T>
IEnumerator<T>
|
表示可以使用foreach 迭代的集合。
|
IEnumerable
IEnumerator
|
KeyedCollection<T, U>
|
表示有键值的集合。
|
KeyedCollection
|
LinkedList<T>
|
表示双向链表。
|
None.
|
LinkedListNode<T>
|
表示LinkedList<T>中的节点。
|
None.
|
List<T>
IList<T>
|
使用大小可按需动态增加的数组实现 IList 接口
|
ArrayList
IList
|
Queue<T>
|
表示对象的先进先出集合。
|
Queue
|
ReadOnlyCollection<T>
|
为泛型只读容器提供基类。
|
ReadOnlyCollectionBase
|
SortedDictionary<K, V>
|
表示键/值对的集合,这些键和值按键排序并可按照键访问,实现IComparer<T>接口。
|
SortedList
|
Stack<T>
|
表示对象的简单的后进先出集合。
|
Stack
|