C#创建List类

首先自己创建一个类
namespace TEST0731
{
    internal class MyList<T>
    {
        T[] list = new T[0];
        int count=0;//list的数量,可加属性去保护
       public void Add(T items)//数组每次超出数量自动加4
        {
            if(list.Length<=count)
            {
                T[] temp=list;
                list = new T[count+4];
                temp.CopyTo(list,0);
            }
            list[count] = items;
            count++;
        }
    }
}

程序里去调用

namespace TEST0731
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        MyList<int> list = new MyList<int>();
        private void Form1_Load(object sender, EventArgs e)
        {
            list.Add(1);    
            list.Add(2);    
            list.Add(3);    
            list.Add(4);
            list.Add(5);
        }
    }
}

 

posted @ 2022-07-31 21:58  奔赴山海Y  阅读(890)  评论(0)    收藏  举报