_C/C++语言_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

C#泛型集合实例应用浅析

作者:黑客防线网安C/C++教程基地 来源:黑客防线网安C/C++教程基地 浏览次数:0

本篇关键词:浅析应用实例集合

    C# 泛型集合了解之前我们明白集合是OOP中的一个重要概念C#中对集合的全面支持更是该语言的精华之一C# 泛型是C# 2.0中的新增元素(C++中称为模板)主要用于解决一系列类似的问题这种机制允许将类名作为参数传递给泛型类型,并生成相应的对象。将泛型(包括类、接口、方法、委托等)看作模板可能更好理解,模板中的变体部分将被作为参数传进来的类名称所代替,从而得到一个新的类型定义。泛型是一个比较大的话题,在此不作详细解析,有兴趣者可以查阅相关资料。
    C# 泛型集合类用起来十分的方便快捷。在这篇随笔里面,我将用链表来模拟c#中的 List﹤T﹥ 类的行为,废话不多说,下面来看我的实现代码,代码中已经写了注释,所以不再对代码进行额外的说明:

    using System.Collections;
    class MyList﹤T﹥
    {
    private MyListNode firstNode;//首节点
    private int count;//C# 泛型集合-节点计数
    public MyList()
    {
    this.firstNode = null;
    this.count = 0;
    }
    //C# 泛型集合-得到List长度
    public int GetLength()
    {
    return this.count;
    }
    //增加一个节点
    public void AddElement(T data)
    {
    MyListNode first = this.firstNode;
    if(first==null)
    {
    this.firstNode=new MyListNode(data);
    this.count++;
    return;
    }
    while (first.next != null)
    {
    first = first.next;
    }
    first.next = new MyListNode(data);
    this.count++;
    }
    //C# 泛型集合-删除一个节点
    public bool Remove(T data)
    {
    MyListNode first = this.firstNode;
    if (first.data.Equals(data))
    {
    this.firstNode = first.next;
    this.count--;
    return true;
    }
    while (first.next!=null)
    {
    if (first.next.data.Equals(data))
    {
    first.next = first.next.next;
    this.count--;
    return true;
    }
    }
    return false;
    }
    //C# 泛型集合-得到指定索引上的集合元素
    public T GetAtIndex(int index)
    {
    int innercount = 1;
    MyListNode first = this.firstNode;
    if (index ﹥ count)
    {
    throw new Exception("Index out of boundary");
    }
  

 

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14999-1.html
网站维护教程更新时间:2012-04-04 22:48:21  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer