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

黄舟
发布: 2016-12-21 14:41:22
原创
1392人浏览过

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");
    }
  ;  else
    {
    while (innercount ﹤ index)
    {
    first = first.next;
    innercount++;
    }
    return first.data;
    }
    }
    //在指定的索引上插入新的元素
    public void insertatindex(int index,t data)
    {
    int innercount = 1;
    mylistnode first = this.firstnode;
    if (index ﹥ count)
    {
    throw new exception("index out of boundary");
    }
    if (index == 1)
    {
    this.firstnode = new mylistnode(data);
    this.firstnode.next = first;
    }
    else
    {
    while (innercount ﹤ index - 1)
    {
    first = first.next;
    innercount++;
    }
    mylistnode newnode = new mylistnode(data);
    newnode.next = first.next;
    first.next = newnode;
    }
    this.count++;
    }
    //c# 泛型集合-删除指定索引上的集合元素
    public void removeatindex(int index)
    {
    int innercount = 1;
    mylistnode first = this.firstnode;
    if (index ﹥ count)
    {
    throw new exception("index out of boundary");
    }
    if (index == 1)
    {
    this.firstnode = first.next;
    }
    else
    {
    while (innercount ﹤ index - 1)
    {
    first = first.next;
    innercount++;
    }
    first.next = first.next.next;
    }
    this.count--;
    }
    //c# 泛型集合-删除集合中的所有元素
    public void removeall()
    {
    this.firstnode = null;
    this.count = 0;
    }
    //为实现该集合类能用foreach进行遍历
    public ienumerator getenumerator()
    {
    mylistnode first = this.firstnode;
    while (first!= null)
    {
    yield return first.data;
    first = first.next;
    }
    }
    //内部节点类
    private class mylistnode
    {
    public t data { get; set; }//节点上的元素值

public mylistnode next { get; set; }//节点的下一个节点
    public mylistnode(t nodedata)
    {
    this.data = nodedata;
    this.next = null;
    }
    }
    }

    下面是c# 泛型集合对这个模拟类的使用:

    class program
    {
    static void main(string[] args)
    {
    mylist﹤string﹥ ml = new mylist﹤string﹥();
    ml.addelement("xu");
    ml.addelement("jin");
    ml.addelement("lin");
    ml.addelement("love");
    ml.addelement("jasmine");
    ml.insertatindex(4, "fiercely");
    ml.removeatindex(2);
    ml.remove("lin");
    foreach (string s in ml)
    {
    console.writeline(s);
    }
    }
    }

    c# 泛型集合实例应用的基本内容就向你介绍到这里,希望对你了解和学习c# 泛型集合有所帮助。

 以上就是C# 泛型集合实例应用浅析的内容,更多相关内容请关注PHP中文网(www.php.cn)!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号