c#2.0泛型学习(一)
根据微软的视频教程"跟我一起学Visual Studio 2005C#语法篇"来学,因为里面有比较多的代码示例,学起来比较容易好理解
1.未使用泛型的Stack类
1using System;
2
3public class Stack
4{
5 readonly int m_Size;
6 int m_StackPointer = 0;
7 object[] m_Items;
8 public Stack(): this(100)
9 { }
10 public Stack(int size)
11 {
12 m_Size = size;
13 m_Items = new object[m_Size];
14 }
15 public void Push(object item)
16 {
17 if (m_StackPointer >= m_Size)
18 throw new StackOverflowException();
19
20 m_Items[m_StackPointer] = item;
21 m_StackPointer++;
22 }
23 public object Pop()
24 {
25 m_StackPointer--;
26 if (m_StackPointer >= 0)
27 {
28 return m_Items[m_StackPointer];
29 }
30 else
31 {
32 m_StackPointer = 0;
33 throw new InvalidOperationException("Cannot pop an empty stack");
34 }
35 }
36}
37
2.使用泛型的类
1using System;
2
3public class Stack<T>
4{
5 readonly int m_Size;
6 int m_StackPointer = 0;
7 T[] m_Items;
8 public Stack()
9 : this(100)
10 {
11 }
12 public Stack(int size)
13 {
14 m_Size = size;
15 m_Items = new T[m_Size];
16 }
17 public void Push(T item)
18 {
19 if (m_StackPointer >= m_Size)
20 throw new StackOverflowException();
21
22 m_Items[m_StackPointer] = item;
23 m_StackPointer++;
24 }
25 public T Pop()
26 {
27 m_StackPointer--;
28 if (m_StackPointer >= 0)
29 {
30 return m_Items[m_StackPointer];
31 }
32 else
33 {
34 m_StackPointer = 0;
35 //throw new InvalidOperationException("Cannot pop an empty stack");
36 return default(T);
37 }
38 }
39}
40
41public class Stack1<T> : Stack<T>
42{
43
44}
45
下为PDF文档,我感觉挺好的,很简单,我听的懂就是好的
/Clingingboy/one.pdf
多个泛型
1class Node<K, T>
2{
3 public K Key;
4 public T Item;
5 public Node<K, T> NextNode;
6 public Node()
7 {
8 Key = default(K);
9 Item = default(T);
10 NextNode = null;
11 }
12 public Node(K key, T item, Node<K, T> nextNode)
13 {
14 Key = key;
15 Item = item;
16 NextNode = nextNode;
17 }
18}
泛型别名
1using list = LinkedList<int, string>;
泛型约束
1public class LinkedList<K, T> where K : IComparable
2{
3 Node<K, T> m_Head;
4 public LinkedList()
5 {
6 m_Head = new Node<K, T>();
7 }
8 public void AddHead(K key, T item)
9 {
10 Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
11 m_Head.NextNode = newNode;
12 }
13
14 T Find(K key)
15 {
16 Node<K, T> current = m_Head;
17 while (current.NextNode != null)
18 {
19 if (current.Key.CompareTo(key) == 0)
20 break;
21 else
22 current = current.NextNode;
23 }
24 return current.Item;
25 }
26
27}
28
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace VS2005Demo1
6{
7 public class MyBaseClassGeneric // sealed,static
8 {
9 }
10
11 interface IMyBaseInterface
12 {
13 void A();
14 }
15
16 internal class GenericClass<T> where T : MyBaseClassGeneric,IMyBaseInterface
17 {
18
19 }
20
21 class GClass<K, T> where K : MyBaseClassGeneric,IMyBaseInterface,new() where T : K
22 {
23
24 }
25
26 class GUClass<K, T> where T : K where K : MyBaseClassGeneric,IMyBaseInterface, new()
27 {
28 GClass<K, T> obj = new GClass<K, T>();
29 }
30
31
32 不能将引用/值类型约束与基类约束一起使用,因为基类约束涉及到类#region 不能将引用/值类型约束与基类约束一起使用,因为基类约束涉及到类
33
34 //class A<T> where T : struct,class
35 //{}
36
37 #endregion
38
39 不能使用结构和默认构造函数约束,因为默认构造函数约束也涉及到类#region 不能使用结构和默认构造函数约束,因为默认构造函数约束也涉及到类
40
41 //class A<T> where T : struct,new()
42 //{}
43
44 #endregion
45
46 虽然您可以使用类和默认构造函数约束,但这样做没有任何价值#region 虽然您可以使用类和默认构造函数约束,但这样做没有任何价值
47
48 class A<T> where T : new()
49 {
50 T obj = new T();
51 }
52
53 class TypeA
54 {
55 public TypeA() { }
56 }
57
58 class TestA
59 {
60 A<TypeA> obj = new A<TypeA>();
61 }
62
63 #endregion
64
65 可以将引用/值类型约束与接口约束组合起来,前提是引用/值类型约束出现在约束列表的开头#region 可以 将引用/值类型约束与接口约束组合起来,前提是引用/值类型约束出现在约束列表的开头
66
67 class SClass<K> where K : struct, IMyBaseInterface
68 { }
69
70 class CClass<K> where K : class, IMyBaseInterface
71 { }
72
73 #endregion
74}
75