0

0

C#基础知识整理 基础知识(17)ILiest接口——泛型

黄舟

黄舟

发布时间:2017-02-11 13:48:08

|

1622人浏览过

|

来源于php中文网

原创

对于arraylist中如果插入值类型会引发装箱操作,而取出值类型又需要拆箱,如下

            ArrayList myArrayList = new ArrayList();

            myArrayList.Add(40);//装箱

            myArrayList.Add(80);//装箱
            
            Int32 a1 = (Int32)myArrayList[0];//拆箱

            Int32 a2 = (Int32)myArrayList[1];//拆箱

从而造成性能的消耗。至于装箱的详细解说见下一篇。
为了解决这些问题,C#中有支持泛型的IList接口,下面看详细代码,其实结构都和IList一样,只是增加了泛型。

 

 /// 
    /// 泛型集合类
    /// 
    /// 
    public class List : IList, IList
    {
        /// 
        /// 泛型迭代器
        /// 
        /// 
        public struct Enumertor : IEnumerator, IEnumerator
        {
            //迭代索引
            private int index;

            //迭代器所属的集合对象引用
            private List list;

            public Enumertor(List container)
            {
                this.list = container;

                this.index = -1;
            }

            public void Dispose()
            {
            }

           /// 
           /// 显示实现IEnumerator的Current属性
           /// 
            object IEnumerator.Current
            {
                get
                {
                    return list[index];
                }
            }

            /// 
            /// 实现IEnumerator的Current属性
            /// 
            public T Current
            {
                get
                {
                    return list[index];
                }
            }

            /// 
            /// 迭代器指示到下一个数据位置
            /// 
            /// 
            public bool MoveNext()
            {
                if (this.index < list.Count)
                {
                    ++this.index;
                }

                return this.index < list.Count;
            }

            public void Reset()
            {
                this.index = -1;
            }
        }

        /// 
        /// 保存数据的数组,T类型则体现了泛型的作用。
        /// 
        private T[] array;

        /// 
        /// 当前集合的长度
        /// 
        private int count;

        /// 
        /// 默认构造函数
        /// 
        public List()
            : this(1)
        {

        }

        public List(int capacity)
        {
            if (capacity < 0)
            {
                throw new Exception("集合初始长度不能小于0");
            }

            if (capacity == 0)
            {
                capacity = 1;
            }

            this.array = new T[capacity];
        }

        /// 
        /// 集合长度
        /// 
        public int Count
        {
            get
            {
                return this.count;
            }
        }

        /// 
        /// 集合实际长度
        /// 
        public int Capacity
        {
            get
            {
                return this.array.Length;
            }
        }

        /// 
        /// 是否固定大小
        /// 
        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        /// 
        /// 是否只读
        /// 
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// 
        /// 是否可同属性
        /// 
        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        /// 
        /// 同步对象
        /// 
        public object SyncRoot
        {
            get
            {
                return null;
            }
        }

        /// 
        /// 长度不够时,重新分配长度足够的数组
        /// 
        /// 
        private T[] GetNewArray()
        {
            return new T[(this.array.Length + 1) * 2];
        }

        /// 
        /// 实现IListAdd方法
        /// 
        /// 
        public void Add(T value)
        {
            int newCount = this.count + 1;

            if (this.array.Length < newCount)
            {
                T[] newArray = GetNewArray();

                Array.Copy(this.array, newArray, this.count);

                this.array = newArray;
            }

            this.array[this.count] = value;

            this.count = newCount;
        }

        /// 
        /// 向集合末尾添加对象
        /// 
        /// 
        /// 
         int IList.Add(object value)
        {
            ((IList)this).Add((T)value);

            return this.count - 1;
        }

        /// 
        /// 实现IList索引器
        /// 
        /// 
        /// 
        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= this.count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                return this.array[index];
            }

            set
            {
                if (index < 0 || index >= this.count)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                this.array[index] = value;
            }
        }

        /// 
        /// 显示实现IList接口的索引器
        /// 
        /// 
        /// 
        object IList.this[int index]
        {
            get
            {
                return ((IList)this)[index];
            }

            set
            {
                ((IList)this)[index] = (T)value;
            }
        }

        /// 
        /// 删除集合中的元素
        /// 
        /// 
        /// 
        public void RemoveRange(int index, int count)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            int removeIndex = index + count;

            if (count < 0 || removeIndex > this.count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);

            this.count -= count;
        }

        /// 
        /// 实现IList接口的indexOf方法
        /// 
        /// 
        /// 
        public int IndexOf(T value)
        {
            int index = 0;

            if (value == null)
            {
                while (index < this.count)
                {
                    if (this.array[index] == null)
                    {
                        return index;
                    }

                    ++index;
                }
            }
            else
            {
                while (index < this.count)
                {
                    if (value.Equals(this.array[index]))
                    {
                        return index;
                    }

                    ++index;
                }
            }
            return -1;
        }

        /// 
        /// 显示实现IList接口的IndexOf方法
        /// 
        /// 
        /// 
         int IList.IndexOf(object value)
        {
            return ((IList)this).IndexOf((T)value);
        }

        /// 
        /// 查找对应数组项
        /// 
        /// 
        /// 
        /// 
        public int IndexOf(object o, IComparer compar)
        {
            int index = 0;

            while (index < this.count)
            {
                if (compar.Compare(this.array[index], o) == 0)
                {
                    return index;
                }

                ++index;
            }

            return -1;
        }

        /// 
        /// 实现IList接口的Remove方法
        /// 
        /// 
        /// 
        public bool Remove(T value)
        {
            int index = this.IndexOf(value);

            if (index >= 0)
            {
                this.RemoveRange(index, 1);

                return true;
            }

            return false;
        }

        /// 
        /// 显示实现IList接口的Remove方法,此处显示实现
        /// 
        /// 
        void IList.Remove(object value)
        {
            ((IList)this).Remove((T)value);
        }

        /// 
        /// 从集合指定位置删除对象的引用
        /// 
        /// 
        public void RemoveAt(int index)
        {
            RemoveRange(index, 1);
        }

        /// 
        /// 弹出集合的最后一个元素
        /// 
        /// 
        public object PopBack()
        {
            object o = this.array[this.count - 1];

            RemoveAt(this.count - 1);

            return o;
        }

        /// 
        /// 弹出集合第一个对象
        /// 
        /// 
        public object PopFront()
        {
            object o = this.array[0];

            RemoveAt(0);

            return o;
        }

        /// 
        /// 实现IList接口的Insert方法
        /// 
        /// 
        /// 
        public void Insert(int index, T value)
        {
            if (index >= this.count)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            int newCount = this.count + 1;

            if (this.array.Length < newCount)
            {
                T[] newArray = GetNewArray();

                Array.Copy(this.array, newArray, index);

                newArray[index] = value;

                Array.Copy(this.array, index, newArray, index + 1, this.count - index);

                this.array = newArray;
            }
            else
            {
                Array.Copy(this.array, index, this.array, index + 1, this.count - index);

                this.array[index] = value;
            }

            this.count = newCount;
        }

        /// 
        /// 显示实现IList接口的Insert方法
        /// 
        /// 
        /// 
        void IList.Insert(int index, object value)
        {
            ((IList)this).Insert(index, (T)value);
        }

        /// 
        /// 实现IList接口的Contains方法
        /// 
        /// 
        /// 
        public bool Contains(T value)
        {
            return this.IndexOf(value) >= 0;
        }

        /// 
        /// 显示实现IList接口的Contains方法
        /// 
        /// 
        /// 
        bool IList.Contains(object value)
        {
            return ((IList)this).IndexOf((T)value) >= 0;
        }

        /// 
        /// 将集合压缩为实际长度
        /// 
        public void TrimToSize()
        {
            if (this.array.Length > this.count)
            {
                T[] newArray = null;

                if (this.count > 0)
                {
                    newArray = new T[this.count];

                    Array.Copy(this.array, newArray, this.count);
                }
                else
                {
                    newArray = new T[1];
                }

                this.array = newArray;
            }
        }

        /// 
        /// 清空集合
        /// 
        public void Clear()
        {
            this.count = 0;
        }

        /// 
        /// 实现IEnumerable接口的GetEnumerator方法
        /// 
        /// 
        public IEnumerator GetEnumerator()
        {
            Enumertor ator = new Enumertor(this);

            return ator;
        }

        /// 
        /// 显示实现IEnumerable接口的GetEnumerator方法
        /// 
        /// 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)this).GetEnumerator();
        }

        /// 
        /// 实现ICollection接口的CopyTo方法
        /// 
        /// 
        /// 
        public void CopyTo(T[] array, int index)
        {
            Array.Copy(this.array, 0, array, index, this.count);
        }

        /// 
        /// 显示实现实现ICollection接口的CopyTo方法
        /// 
        /// 
        /// 
        void ICollection.CopyTo(Array array, int index)
        {
            Array.Copy(this.array, 0, array, index, this.count);
        }
    }

调用:

AI Room Planner
AI Room Planner

AI 室内设计工具,免费为您的房间提供上百种设计方案

下载
 static void Main(string[] args)
        {
            //由于已经指定了int,因此加入值类型不会有装箱拆箱操作。
            List tList = new List();

            tList.Add(25);

            tList.Add(30);

            foreach (int n in tList)
            {
                Console.WriteLine(n);
            }

            Console.ReadLine();
        }

以上就是C#基础知识整理 基础知识(17)ILiest接口——泛型 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

14

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

12

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

4

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

18

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
【web前端】Node.js快速入门
【web前端】Node.js快速入门

共16课时 | 2万人学习

Go语言实战之 GraphQL
Go语言实战之 GraphQL

共10课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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