为什么使用泛型
泛型提供了更好的性能,因为它们还会导致装箱或拆箱的损耗
泛型更类型安全,因为它们只包含我们指定的类型
泛型大幅减少了构建自定义集合类型的需要,因为基础类库提供了几个预制的容器。
T占位符
List<T>
KeyValuePair<TKey,TValue>
尖括号中标记的正式名称为类型参数,也叫占位符。<T>符号读作of T。因此List<T>读作List of T,或者也可以称其为类型T的枚举。
一般情况下T表示类型,TKey或K表示键,TValue或V表示值。
泛型方法
#region static void Swap<T>(ref T a, ref T b) 交换两个数据
/// <summary>
/// 交换两个数据
/// </summary>
/// <typeparam name="T">类型参数</typeparam>
/// <param name="a">参数</param>
/// <param name="b">参数</param>
static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
//int a =1, b = 2;
//Console.WriteLine("a:{0},b:{1}", a, b);
// 1 2
//Swap<int>(ref a, ref b);
//Console.WriteLine("a:{0},b:{1}", a, b);
// 2 1
//string strA = "1", strB = "2";
//Console.WriteLine("strA:{0},strB:{1}", strA, strB);
// 1 2
//Swap<string>(ref strA, ref strB);
// 2 1
//Console.WriteLine("strA:{0},strB:{1}", strA, strB);
}
#endregion
defaut参数
用于设置泛型中T的默认的参数类型:
数值为0;
引用类型为null;
结构字段为0或null
public struct Point<T>
{
T xPos;
T yPos;
public T X
{
get { return xPos; }
set { xPos = value; }
}
public T Y
{
get { return yPos; }
set { yPos = value; }
}
public Point(T xVal,T yVal)
{
this.yPos = yVal;
this.xPos = xVal;
}
public override string ToString()
{
return string.Format("[{0},{1}]",xPos,yPos);
}
public void ResetPoint()
{
xPos=default(T);
yPos=default(T);
}
}
where
用于类型参数的约束
where T:struct 必须为结构
where T:class 必须为引用类型
where T:new() 必须包含一个默认的构造函数。此约束必须列在末尾。
where T:NameOfBaseClass 该类型参数<T>必须派生于NameOfBaseClass
where T:NameOfInterface 该类型参数<T>必须派生于NameOfInterface
可空类型
可以让值类型为空,这对于数据库来说是很多用的。
int? nullableInt=null;
泛型提供了更好的性能,因为它们还会导致装箱或拆箱的损耗
泛型更类型安全,因为它们只包含我们指定的类型
泛型大幅减少了构建自定义集合类型的需要,因为基础类库提供了几个预制的容器。
T占位符
List<T>
KeyValuePair<TKey,TValue>
尖括号中标记的正式名称为类型参数,也叫占位符。<T>符号读作of T。因此List<T>读作List of T,或者也可以称其为类型T的枚举。
一般情况下T表示类型,TKey或K表示键,TValue或V表示值。
泛型方法
#region static void Swap<T>(ref T a, ref T b) 交换两个数据
/// <summary>
/// 交换两个数据
/// </summary>
/// <typeparam name="T">类型参数</typeparam>
/// <param name="a">参数</param>
/// <param name="b">参数</param>
static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
//int a =1, b = 2;
//Console.WriteLine("a:{0},b:{1}", a, b);
// 1 2
//Swap<int>(ref a, ref b);
//Console.WriteLine("a:{0},b:{1}", a, b);
// 2 1
//string strA = "1", strB = "2";
//Console.WriteLine("strA:{0},strB:{1}", strA, strB);
// 1 2
//Swap<string>(ref strA, ref strB);
// 2 1
//Console.WriteLine("strA:{0},strB:{1}", strA, strB);
}
#endregion
defaut参数
用于设置泛型中T的默认的参数类型:
数值为0;
引用类型为null;
结构字段为0或null
public struct Point<T>
{
T xPos;
T yPos;
public T X
{
get { return xPos; }
set { xPos = value; }
}
public T Y
{
get { return yPos; }
set { yPos = value; }
}
public Point(T xVal,T yVal)
{
this.yPos = yVal;
this.xPos = xVal;
}
public override string ToString()
{
return string.Format("[{0},{1}]",xPos,yPos);
}
public void ResetPoint()
{
xPos=default(T);
yPos=default(T);
}
}
where
用于类型参数的约束
where T:struct 必须为结构
where T:class 必须为引用类型
where T:new() 必须包含一个默认的构造函数。此约束必须列在末尾。
where T:NameOfBaseClass 该类型参数<T>必须派生于NameOfBaseClass
where T:NameOfInterface 该类型参数<T>必须派生于NameOfInterface
可空类型
可以让值类型为空,这对于数据库来说是很多用的。
int? nullableInt=null;