《CLR Via C# 第3版》笔记之(十三) - 泛型基础

简介:

.net2.0开始就引入了泛型的机制,泛型有助于我们实现“算法重用”。

借助于泛型机制,我们可以少定义一些重载函数,同时还能保证类型安全性。泛型的语法非常简单,下面通过例子来演示泛型的一些应用。

主要内容:

  • 减少装箱/拆箱(提高性能)
  • 限制泛型参数的类型
  • 节点类型不同的链表

1. 减少装箱/拆箱(提高性能)

我们都知道,.net中的额装箱/拆箱操作非常损害性能,通过使用泛型,可以有效的减少我们代码中的装箱拆箱操作,从而提高代码的性能。

实例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using  System;
using  System.Collections.Generic;
using  System.Collections;
 
class  CLRviaCSharp_13
{
     static  void  Main( string [] args)
     {
         List<Int32> lst = new  List< int >();
         lst.Add(1);
         Int32 i = lst[0];
 
         ArrayList arr = new  ArrayList();
         arr.Add(1);
         // 此处必须强制转型,否则报错,
         // 因为ArrayList中的元素都是Object类型的。
         i = (Int32) arr[0];
     }
}

代码非常简单,分别用泛型List和ArrayList来存储值类型,然后在取出值类型。

使用泛型List的话,不会出现装箱/拆箱的操作。具体证据还是看下面的IL代码:

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
35
36
37
38
39
40
.method private static hidebysig
     void Main (
         string[] args
     ) cil managed
{
     // Method begins at RVA 0x217c
     // Code size 56 (0x38)
     .maxstack 2
     .entrypoint
     .locals init (
         [0] class [mscorlib]System.Collections.Generic.List`1<int32> lst
         [1] int32 i
         [2] class [mscorlib]System.Collections.ArrayList arr
     )
 
     IL_0000: nop
     IL_0001: newobj instance void [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
     IL_0006: stloc.0
     IL_0007: ldloc.0
     IL_0008: ldc.i4.1
     IL_0009: callvirt instance void [mscorlib]System.Collections.Generic.List`1<int32>::Add(!!0)
     IL_000e: nop
     IL_000f: ldloc.0
     IL_0010: ldc.i4.0
     IL_0011: callvirt instance !!0 [mscorlib]System.Collections.Generic.List`1<int32>::get_Item(int32)
     IL_0016: stloc.1
     IL_0017: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()
     IL_001c: stloc.2
     IL_001d: ldloc.2
     IL_001e: ldc.i4.1
     IL_001f: box int32
     IL_0024: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
     IL_0029: pop
     IL_002a: ldloc.2
     IL_002b: ldc.i4.0
     IL_002c: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
     IL_0031: unbox.any int32
     IL_0036: stloc.1
     IL_0037: ret
} // End of method CLRviaCSharp_13.Main

其中IL_0001~IL_0016是泛型List的相关操作

IL_0017~IL_0036是ArrayList的操作,包含的损害性能的装箱(IL_001f: box int32)和拆箱(IL_0031: unbox.any int32)操作。

2.  限制泛型参数的类型

首先有一点需要说明,泛型类型和普通类型在静态构造函数上有一点不同。

对于普通类型,静态构造函数只在此类型第一次初始化的时候才会执行,

而泛型类型的静态构造函数会在 每种特定类型(即泛型参数T被替换为Int32或者String等等)的第一次初始化的时候执行。

描述的有些拗口,还是看代码吧:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using  System;
using  System.Collections.Generic;
using  System.Collections;
using  System.Threading;
 
 
class  CLRviaCSharp_13
{
     static  void  Main( string [] args)
     {
         /*
          * 非泛型的类:虽然有3次初始化,但是静态构造函数只执行一次
          */
         // 第一次初始化,会执行静态构造函数
         NormalClass nc = new  NormalClass();
         // 第二次初始化,不会执行静态构造函数
         NormalClass nc2 = new  NormalClass();
         // 第三次初始化,不会执行静态构造函数
         NormalClass nc3 = new  NormalClass();
 
         /*
          * 泛型的类:泛型参数类型改变的话,会再次执行静态构造函数
          */
         // 对泛型参数(string)来说是第一次初始化,会执行静态构造函数
         GenericClass< string > gc = new  GenericClass< string >();
         // 对泛型参数(string)来说是第二次初始化,不会执行静态构造函数
         GenericClass< string > gc2 = new  GenericClass< string >();
         // 对泛型参数(Int32)来说是第一次初始化,会执行静态构造函数
         GenericClass<Int32> gc3 = new  GenericClass<Int32>();
 
         Console.ReadKey( true );
     }
}
 
public  class  GenericClass<T>
{
     static  GenericClass()
     {
         Thread.Sleep(1000);
         Console.WriteLine( "GenericClass<"  + typeof (T) + "> is initialized at : "  + DateTime.Now.ToString( "yyyy/MM/dd HH:mm:ss fff" ));
     }
}
 
public  class  NormalClass
{
     static  NormalClass()
     {
         Thread.Sleep(1000);
         Console.WriteLine( "NormalClass is initialized at : "  + DateTime.Now.ToString( "yyyy/MM/dd HH:mm:ss fff" ));
     }
}

以上代码的执行结果为:

image

根据泛型类型的静态构造的特点,我们可以通过泛型类型的静态构造函数来限制泛型参数(T)的类型。

比如以下代码,通过泛型类型的静态构造函数来限制泛型参数(T)只能为值类型

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
using  System;
 
class  CLRviaCSharp_13
{
     static  void  Main( string [] args)
     {
         // 对于引用类型的泛型参数,会抛出异常
         GenericClass< string > gc = new  GenericClass< string >();
         // 对于值类型的泛型参数,都能正常初始化
         GenericClass<Int32> gc2 = new  GenericClass<Int32>();
         GenericClass<Double> gc3 = new  GenericClass<Double>();
         GenericClass<DateTime> gc4 = new  GenericClass<DateTime>();
 
         Console.ReadKey( true );
     }
}
 
public  class  GenericClass<T>
{
     static  GenericClass()
     {
         if  (! typeof (T).IsValueType)
         {
             throw  new  ArgumentException( "T must be a Enum type!" );
         }
 
         Console.WriteLine( "Type "  + typeof (T).ToString() + " is initilized!" );
     }
}

注释掉 GenericClass<string> gc = new GenericClass<string>(); 就能正常执行。

关于泛型参数(T)的限制,将在下一篇 泛型高级 中有更进一步的阐释。

3.  节点类型不同的链表

链表是一种常用的数据结构,以往构造链表时,每个节点往往都是相同的类型,否则取出节点后我们无法还原其本身的类型。

但是现在借助于泛型,我们可以构造出节点类型不同的链表,而且链表中每个节点都是强类型(不是Object类型)的,从而满足日益复杂的需求。

代码如下:

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
35
36
37
38
39
40
41
42
43
using  System;
 
class  CLRviaCSharp_13
{
     static  void  Main( string [] args)
     {
         Node header = new  TypedNode<Char>( '.' );
         header = new  TypedNode< string >( "hello world" , header);
         header = new  TypedNode<Int32>(100, header);
         header = new  TypedNode<DateTime>(DateTime.Now, header);
 
         Console.WriteLine(header.ToString());
         Console.ReadKey( true );
     }
}
 
public  class  Node
{
     protected  Node _next;
 
     public  Node(Node next)
     {
         _next = next;
     }
}
 
public  sealed  class  TypedNode<T> : Node
{
     public  T _data;
     public  TypedNode(T data) : this (data, null )
     {}
 
     public  TypedNode(T data, Node next) : base (next)
     {
         _data = data;
     }
 
     public  override  string  ToString()
     {
         return  _data.ToString() + "\n"  +
             ((_next != null ) ? _next.ToString() : null );
     }
}

这个例子是《CLR via C#》上的,每次都是从链表头部增加一个节点。实际应用时也可以根据需求修改为从链表尾部追加节点。

执行结果如下:

image

标签:  CLR via C#笔记



本文转自wang_yb博客园博客,原文链接:http://www.cnblogs.com/wang_yb/archive/2011/07/19/2110808.html,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
C# Python
C# 笔记1 - 操作目录
C# 笔记1 - 操作目录
29 0
|
3月前
|
存储 安全 编译器
C# 11.0中的泛型属性:类型安全的新篇章
【1月更文挑战第23天】C# 11.0引入了泛型属性的概念,这一新特性为开发者提供了更高级别的类型安全性和灵活性。本文将详细探讨C# 11.0中泛型属性的工作原理、使用场景以及它们对现有编程模式的改进。通过深入了解泛型属性,开发者将能够编写更加健壮、可维护的代码,并充分利用C#语言的最新发展。
|
7月前
|
存储 算法 安全
C#三十二 泛型的理解和使用
C#三十二 泛型的理解和使用
21 0
|
1月前
|
存储 安全 Java
34.C#:listT泛型集合
34.C#:listT泛型集合
17 1
|
1月前
|
开发框架 安全 .NET
C# .NET面试系列三:集合、异常、泛型、LINQ、委托、EF!
<h2>集合、异常、泛型、LINQ、委托、EF! #### 1. IList 接口与 List 的区别是什么? IList 接口和 List 类是C#中集合的两个相关但不同的概念。下面是它们的主要区别: <b>IList 接口</b> IList 接口是C#中定义的一个泛型接口,位于 System.Collections 命名空间。它派生自 ICollection 接口,定义了一个可以通过索引访问的有序集合。 ```c# IList 接口包含一系列索引化的属性和方法,允许按索引访问、插入、移除元素等。 由于是接口,它只定义了成员的契约,而不提供具体的实现。类似于 IEnumera
154 2
|
3月前
|
C# Python
C# 笔记3 - 重载一系列像python那样的print()方法
C# 笔记3 - 重载一系列像python那样的print()方法
27 1
|
3月前
|
存储 C# C++
C# 笔记2 - 数组、集合与与文本文件处理
C# 笔记2 - 数组、集合与与文本文件处理
46 0
|
3月前
|
存储 安全 算法
C# 泛型:类型参数化的强大工具
【1月更文挑战第7天】本文将深入探讨C#语言中的泛型编程,包括泛型的定义、用途、优势以及实际应用。通过类型参数化,泛型允许开发者编写更加灵活且可重用的代码,同时提高程序的类型安全性和性能。本文将通过示例代码和详细解释,帮助读者更好地理解泛型在C#中的重要性和实用性。
|
4月前
|
存储 Java 编译器
【从Java转C#】第五章:泛型
【从Java转C#】第五章:泛型
|
7月前
|
机器学习/深度学习 存储 缓存
一文带你搞懂C#泛型
泛型是.net 2.0中提供的新特性,是框架的一种升级,用于处理用一个事物来代替多种不同需求的情况。下面我们就一块来看一下具体的讲解吧。