c# List集合类常用操作:二、增加

所有操作基于以下类

复制代码

class Employees
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public DateTime BirthDate { get; set; }
}

复制代码

复制代码

Add(T) 将对象添加到 List<T> 的结尾处。
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 0, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 1, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 2, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 3, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 4, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });

复制代码

复制代码

AddRange(IEnumerable<T>)将指定集合的元素添加到 List<T> 的末尾。

复制代码

Employees[] employees =
{
    new Employees { Id = 0, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") },
    new Employees { Id = 1, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") },
    new Employees { Id = 2, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") },
    new Employees { Id = 3, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") },
    new Employees { Id = 4, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") }
};
List<Employees> emp = new List<Employees>();
emp.AddRange(employees);

复制代码

复制代码

复制代码

Insert(Int32, T) 将元素插入 List<T> 的指定索引处。

复制代码

 List<Employees> employees = new List<Employees>();
 employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
 employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
 employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
 employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
 employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });
 //在第一个元素插入
 employees.Insert(0,new Employees { Id = 0, Name = "Michael.Suyama", City = "London", BirthDate = Convert.ToDateTime("1963-07-02") });

复制代码

复制代码

复制代码

InsertRange(Int32, IEnumerable<T>) 将集合中的元素插入 List<T> 的指定索引处。

复制代码

 List<Employees> employees = new List<Employees>();
 employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
 employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
 Employees[] emp =
 {
     new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") },
     new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") },
     new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") }
 };
 employees.InsertRange(2,emp);

复制代码

复制代码

List<T>在C#中只有修改实例的void Add(T item)方法将单个项添加到列表中。
IEnumarable<T> Append(this IEnumerable<T> source, T element)另一方面,是在IEnumerable接口上定义的扩展方法(由所有列表实现)。它不会修改原始列表实例,但会返回一个新的枚举,它将在序列的末尾生成指定的元素。

Append<TSource>(IEnumerable<TSource>, TSource) 将一个值追加到序列末尾。 
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees = employees.Append(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") }).ToList();

复制代码

Prepend<TSource>(IEnumerable<TSource>, TSource) 向序列的开头添加值。
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees = employees.Prepend(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") }).ToList();

输出

//Id Name            City     BirthDate
//---------------------------------------------
//3  Janet.Leverling Kirkland 1963/8/30 0:00:00
//1  Nancy.Davolio   Seattle  1948/12/ 8 0:00:00
//2  Andrew.Fuller   Tacoma   1952/2/ 19 0:00:00 

复制代码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值