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 

posted @ 2020-04-24 10:08  liessay  阅读(13470)  评论(0编辑  收藏  举报