代码改变世界

c#2.0泛型学习(一)

  Clingingboy  阅读(1211)  评论(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<intstring>;

泛型约束

 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    不能将引用/值类型约束与基类约束一起使用,因为基类约束涉及到类
38
39    不能使用结构和默认构造函数约束,因为默认构造函数约束也涉及到类
45
46    虽然您可以使用类和默认构造函数约束,但这样做没有任何价值
64
65    可以将引用/值类型约束与接口约束组合起来,前提是引用/值类型约束出现在约束列表的开头
74}

75


记录一下示例代码
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示