C#自定义List类

本文详细介绍了如何在C#中自定义一个List类,通过代码示例展示了自定义过程,包括构造函数、添加元素、遍历等关键功能的实现。
摘要由CSDN通过智能技术生成

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyArrayList
{    
    public class MyArrayList
    {
        //容量
        private const int _defaultCapacity = 4;
        //存放数组元素
        private object[] _items;
        //数组大小
        private int _size;
        //元素个数为0的数组状态
        private static readonly object[] emptyArray = new object[0];

        /// <summary>
        /// 析构函数(为了不引发未将对象引用到实例,所以对_items赋值)
        /// </summary>
        public MyArrayList()
        {
            this._items = emptyArray;
        }

        public MyArrayList(int capacity) 
        {
            if(capacity<0)
                throw new ArgumentOutOfRangeException("capacity","ArrayList的容量不可为负数");
            this._items = new object[capacity];
        }

        /// <summary>
        /// 索引器
        /// </summary>
        //
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值