C#泛型

释放双眼,带上耳机,听听看~!

泛型

官方:延迟编写类或方法中的编程元素的数据类型,直到实际在程序中使用它。
个人理解:泛型就是一个可以与任何数据类型一起工作的类或方法。换句话说,就是用同一个方法来处理多个方法中功能相似但传入不同的数据类型

为什么使用泛型?

先说不用泛型,下面来举一个例子


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1public class CommonMethod
2{
3    public static void ShowInt(int i)
4    {
5        Console.WriteLine("This is {0},type={1},parameter={2}",
6            typeof(CommonMethod).Name, i.GetType().Name, i);
7    }
8
9    public static void ShowString(string s)
10    {
11        Console.WriteLine("This is {0},type={1},parameter={2}",
12            typeof(CommonMethod).Name, s.GetType().Name, s);
13    }
14
15    public static void ShowDateTime(DateTime d)
16    {
17        Console.WriteLine("This is {0},type={1},parameter={2}",
18            typeof(CommonMethod).Name, d.GetType().Name, d);
19    }
20}
21
22

结果


1
2
3
4
5
6
1***********CommonMethod***************
2This is CommonMethod,type=Int32,parameter=123
3This is CommonMethod,type=String,parameter=456
4This is CommonMethod,type=DateTime,parameter=2019/5/28 11:44:34
5
6

从上面的结果中我们可以看出这三个方法,除了传入的参数不同外,其里面实现的功能都是一样的。那如何进行优化呢?相信很多人会想到了OOP三大特性之一的继承,我们知道,C#语言中,object是所有类型的基类,下面用object进行优化


1
2
3
4
5
6
7
1public static void ShowObject(object o)
2{
3    Console.WriteLine("This is {0},type={1},parameter={2}",
4        typeof(CommonMethod), 0.GetType().Name, o);
5}
6
7

结果


1
2
3
4
5
6
1***********Object***************
2This is ConsoleApp3.CommonMethod,type=Int32,parameter=123
3This is ConsoleApp3.CommonMethod,type=String,parameter=456
4This is ConsoleApp3.CommonMethod,type=DateTime,parameter=2019/5/28 11:55:35
5
6

从上述代码中,我们可以发现,object满足了要求,并且减少了代码冗余,但是却存在装箱和拆箱操作,损耗程序的性能。使用泛型会怎么样呢?


1
2
3
4
5
6
7
1public static void Show<T>(T t)
2{
3    Console.WriteLine("This is {0},type={1},parameter={2}",
4        typeof(CommonMethod), t.GetType().Name, t.ToString());
5}
6
7

结果


1
2
3
4
5
6
1***********Generic***************
2This is ConsoleApp3.CommonMethod,type=Int32,parameter=123
3This is ConsoleApp3.CommonMethod,type=String,parameter=456
4This is ConsoleApp3.CommonMethod,type=DateTime,parameter=2019/5/28 12:05:33
5
6

调用


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace ConsoleApp3
8{
9    class Program
10    {
11        static void Main(string[] args)
12        {
13            int a = 123;
14            string b = "456";
15            DateTime c = DateTime.Now;
16
17            Console.WriteLine("***********CommonMethod***************");
18            CommonMethod.ShowInt(a);
19            CommonMethod.ShowString(b);
20            CommonMethod.ShowDateTime(c);
21            Console.WriteLine("***********Object***************");
22            CommonMethod.ShowObject(a);
23            CommonMethod.ShowObject(b);
24            CommonMethod.ShowObject(c);
25            Console.WriteLine("***********Generic***************");
26            CommonMethod.Show<int>(a);
27            CommonMethod.Show<string>(b);
28            CommonMethod.Show<DateTime>(c);
29            Console.ReadKey();
30        }
31    }
32}
33
34

结果


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1***********CommonMethod***************
2This is CommonMethod,type=Int32,parameter=123
3This is CommonMethod,type=String,parameter=456
4This is CommonMethod,type=DateTime,parameter=2019/5/28 12:11:57
5***********Object***************
6This is ConsoleApp3.CommonMethod,type=Int32,parameter=123
7This is ConsoleApp3.CommonMethod,type=String,parameter=456
8This is ConsoleApp3.CommonMethod,type=DateTime,parameter=2019/5/28 12:11:57
9***********Generic***************
10This is ConsoleApp3.CommonMethod,type=Int32,parameter=123
11This is ConsoleApp3.CommonMethod,type=String,parameter=456
12This is ConsoleApp3.CommonMethod,type=DateTime,parameter=2019/5/28 12:11:57
13
14

为什么泛型可以解决上面的问题呢?

泛型是延迟声明的:即定义的时候没有指定具体的参数类型,把参数类型的声明推迟到了调用的时候才指定参数类型。 延迟思想在程序架构设计的时候很受欢迎。例如:分布式缓存队列、EF的延迟加载等等。

泛型究竟是如何工作的呢?

控制台程序最终会编译成一个exe程序,exe被点击的时候,会经过JIT(即时编译器)的编译,最终生成二进制代码,才能被计算机执行。泛型加入到语法以后,VS自带的编译器又做了升级,升级之后编译时遇到泛型,会做特殊的处理:生成占位符。再次经过JIT编译的时候,会把上面编译生成的占位符替换成具体的数据类型。
C#泛型

给TA打赏
共{{data.count}}人
人已打赏
安全技术

网站制作需要素材的实用网站

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索