泛型(一)
2016-04-10 16:16
141 查看
为什么使用泛型
泛型提供了更好的性能,因为它们还会导致装箱或拆箱的损耗
泛型更类型安全,因为它们只包含我们指定的类型
泛型大幅减少了构建自定义集合类型的需要,因为基础类库提供了几个预制的容器。
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;
相关文章推荐
- python:mysql查询
- 软件工程---做汉堡,结对2.0
- EM算法在直线分类与灭点检测中的应用(关于一篇文章的读后感)
- 完美解决 手机软键盘遮挡问题
- Andriod中Service的生命周期
- asp.net中子级Repeater获取父级Repeater绑定项的值 Eval
- SDAU 搜索专题 20 Prime Ring Problem
- APP性能测试
- DFS&BFS--最大乘积
- C++的一些零散知识
- 利用百度地图WEB服务APIGeoCoding API批量地址解析
- Java排序算法——快速排序
- 夺命雷公狗---Thinkphp----4之数据表的设计
- Javascript全局变量var与不var的区别
- java保留两位小数
- 如何在CSDN博客中编辑公式?
- 初始化MySQL
- matlab中save,load使用方法
- linux下组管理命令详解
- 【CentOS7-Python系列】之一【Python升级】