程序员学习之路一 泛型列表List<T>基本用法

本文详细介绍了如何使用C#中的List进行数据的添加、删除及读取操作,并提供了三种不同的添加数据方法及其示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//using System.Collections.Generic; 命名空间中的List<T>
//using System.Collections; 命名空间中的ArrayList 

建立一个类型的类,就好像int类型

    class po
    {

      public  string test; 要为public 才能访问 不能是私有
        public po(string test)
        {
            this.test = test;
        }

    }

为list 添加数据

方法一   List<po> pos = new List<po>(new po[]{new po("1"),new po("s")});

方法二   po p1 = new po("1");
        po p2 = new po("2");
        po p3 = new po("3");
        List<po> pos = new List<po>(4);
        pos.Add(p1);
        pos.Add(p2);
        pos.Add(p3);

方法三     List<po> pos = new List<po>(4);
        pos.AddRange(new po[]{new po("1"),new po("2")});

删除数据

一       RemoveRange()删除一个范围
           第一个参数 开始位置 第二个 个数
          pos.RemoveRange( 1 , 2 );

读取数据

    foreach (po item in pos)
        {
            TextBox1.Text += item.a.ToString();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值