为什么要使用泛型
我们在编写程序的时,经常遇到两个模块的功能很相似,只是对应处理的数据类型不同,有int,string或者其他自定义的类型,我们起初的做法是什么,定义多个方法去处理每个不同的数据类型,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class CommonMethod
{
/// <summary>
/// 打印int
/// </summary>
/// <param name="value"></param>
public static void showInt(int value) {
Console.WriteLine("方法名:{0},参数类型:{1},值:{2}",
typeof(CommonMethod).Name,value.GetType().Name,value);
}
/// <summary>
/// 打印string
/// </summary>
/// <param name="value"></param>
public static void showString(string value)
{
Console.WriteLine("方法名:{0},参数类型:{1},值:{2}",
typeof(CommonMethod).Name, value.GetType().Name, value);
}
/// <summary>
/// 打印DateTime
/// </summary>
/// <param name="value"></param>
public static void showDateTime(DateTime value)
{
Console.WriteLine("方法名:{0},参数类型:{1},值:{2}",
typeof(CommonMethod).Name, value.GetType().Name, value);
}
}
}
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
CommonMethod.showInt(123);
CommonMethod.showString("abc");
CommonMethod.showDateTime(DateTime.Now);
}
}
}
泛型出现于.NET Framework2.0,那在此之前,1.0时代是如何处理这个问题的,如下,使用Object
static void Main(string[] args)
{
showObject(123);
showObject("abc");
showObject(DateTime.Now);
}
public static void showObject(Object a)
{
Console.WriteLine("方法名:{0},参数类型:{1},值:{2}",
typeof(CommonMethod).Name, value.GetType().Name, value);
}
可以看出效果一样,也确实简化了代码,这利用C#语法的特性,也可以说是一些高级编程语言的特性,那就是Object是所有类型的基类,但这样用有明显问题和缺陷:
会出现装箱、拆箱操作,这将在托管堆上分配和回收大量的变量,若数据量大,性能损失会非常严重;
在处理引用类型时,虽没有装箱、拆箱的操作,但会用到数据类型的强制转换,增加处理器的负担。
因此在.NET Framework2.0增加了泛型的语法使用
泛型的介绍
C#中泛型能够将类型作为参数来传递,即在创建类型时用一个特定的符号(一般为“T”)来作为一个占位符,代替实际的类型,等待在实例化时用一个实际的类型来代替
static void Main(string[] args)
{
show(123);
show("abc");
show(DateTime.Now);
}
public static void show<T>(T value)
{
Console.WriteLine("{0},参数类型:{1},值:{2}",
typeof(Program).Name, value.GetType().Name, value);
}
看起来跟用Object没什么区别,那我们来看看使用泛型的优点:
使用泛型能够最大限度地的重用代码、保护类型安全及提高性能
降低强制转换和装箱拆箱操作的成本和风险
可以对泛型类进行约束以访问特定数据类型的方法
为什么泛型能够解决上面的问题?
泛型是延迟声明的:即定义的时候没有指定具体的参数类型,把参数类型的声明推迟到了调用的时候才指定。延迟思想常用在程序架构设计中。
泛型是如可工作的?
控制台程序最终会编译成一个exe程序,exe被点击的时候,会经过JIT(即时编译)的编译,最终生成二进制代码,才能被计算机执行。泛型加入到语法后,VS自带的编译器又做了升级,升级之后编译遇到泛型,会做特殊处理,即生成占位符。再次经过JIT编译的时候,会把上面编译生成的占位符替换成具体的数据类型,如下:
Console.WriteLine(typeof(List<>));
Console.WriteLine(typeof(Dictionary<,>));
泛型性能问题
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Compare
{
public static void show()
{
int value = 123;
long result1 = 0;
long result2 = 0;
long result3 = 0;
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 10000000; i++)
{
ShowInt(value);
}
watch.Stop();
result1 = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 10000000; i++)
{
ShowObject(value);
}
watch.Stop();
result2 = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 10000000; i++)
{
Show<int>(value);
}
watch.Stop();
result3 = watch.ElapsedMilliseconds;
}
Console.WriteLine("result1={0},result2={1},result3={2}"
, result1, result2, result3);
}
#region PrivateMethod
private static void ShowInt(int iParameter)
{
//do nothing
}
private static void ShowObject(object oParameter)
{
//do nothing
}
private static void Show<T>(T tParameter)
{
//do nothing
}
#endregion
}
}
可以看出性能是泛型>普通>object