C# List集合类常用操作

C# List集合类常用操作 (一)

所有操作基于以下类

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

List()初始化 List 类的新实例,该实例为空并且具有默认初始容量。

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") });

List(IEnumerable) 初始化 List 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。

List<Employees> employees = new List<Employees>()
{
    new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") },
    new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") },
    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") }
}; 
//显示
Console.WriteLine("Employees Report");
Console.WriteLine("Id Name\t\t\tCity\tBirthDate");
Console.WriteLine("-----------------------------------------");
foreach (var item in employees)
{
    Console.WriteLine("{0}  {1}\t{2}\t{3}", item.Id, item.Name, item.City, item.BirthDate);
}

C# List集合类常用操作

List(Int32) 初始化 List 类的新实例,该实例为空并且具有指定的初始容量

List<Employees> employees = new List<Employees>(2);
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") });
Console.WriteLine("Capacity:" + employees.Capacity);  //Capacity:8
Console.WriteLine("Count:" + employees.Count);        //Count:5
//Capacity 列表之前设定的容量值,每超过容量值在此前基础上*2,故0-1=2,2-3=4,4=8,可以通过调用TrimExcess方法或通过显式设置Capacity属性来减少多余容量
//Count 实际的元素个数 

元素操作

List<Employees> employees = new List<Employees>(5);
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") });
Console.WriteLine("初始属性");
Console.WriteLine("Capacity:" + employees.Capacity); 
Console.WriteLine("Count:" + employees.Count);
Console.WriteLine();

Console.WriteLine("添加一条数据");
employees.Add(new Employees { Id = 5, Name = "Michael.Suyama", City = "London", BirthDate = Convert.ToDateTime("1963-07-02") });
Console.WriteLine("Capacity:" + employees.Capacity);
Console.WriteLine("Count:" + employees.Count);
Console.WriteLine();

Console.WriteLine("清除多占用元素总数");
employees.TrimExcess();
Console.WriteLine("Capacity:" + employees.Capacity);
Console.WriteLine("Count:" + employees.Count);

C# List集合类常用操作

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 的结尾处。

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)将指定集合的元素添加到 List 的末尾。

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 的指定索引处。

 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);

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

 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在C#中只有修改实例的void Add(T item)方法将单个项添加到列表中。

IEnumarable Append(this IEnumerable source, T element)另一方面,是在IEnumerable接口上定义的扩展方法(由所有列表实现)。它不会修改原始列表实例,但会返回一个新的枚举,它将在序列的末尾生成指定的元素。

Append(IEnumerable, 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(IEnumerable, 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 

List集合类常用操作:三、查找

List集合查询数据

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") });

All(IEnumerable, Func<TSource,Boolean>)确定序列中的所有元素是否都满足条件。

Any(IEnumerable) 确定序列是否包含任何元素。

//表示集合中的任一个元素满足表达式条件,即返回true。
var res1 = employees.Any(e => e.Id == 3 || e.Id == 8);
Console.WriteLine(res1);  //out:True

var res2 = employees.Any(e => e.Id == 3 && e.Name == "Steven");
Console.WriteLine(res2);  //out:False  数据源Name中没有Steven

Contains(T) 确定某元素是否在 List 中。

Equals(Object) 确定指定的对象是否等于当前对象。(继承自 Object)

Exists(Predicate) 确定 List 是否包含与指定谓词定义的条件匹配的元素。

Find(Predicate) 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的第一个匹配元素。

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") });

//如果找到与指定谓词定义的条件匹配的第一个元素,则为该元素;
var emp = employees.Find(e => e.Id == 5);
Console.WriteLine(emp.Id);          //5
Console.WriteLine(emp.Name);        //Steven.Buchanan
Console.WriteLine(emp.City);        //London
Console.WriteLine(emp.BirthDate);   //1955-03-04

FindAll(Predicate) 检索与指定谓词定义的条件匹配的所有元素。

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") });

//如果找到一个 List<T>,其中所有元素均与指定谓词定义的条件匹配,则为该数组;否则为一个空
var emp = employees.FindAll(e => e.Id >=3);//遍历访问
foreach (var item in emp)
{
    Console.WriteLine(item.Id);          
    Console.WriteLine(item.Name);        
    Console.WriteLine(item.City);        
    Console.WriteLine(item.BirthDate);   
}

FindIndex(Predicate) 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中第一个匹配元素的从零开始的索引。

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") });

//如果找到与 match 定义的条件相匹配的第一个元素,则为该元素的从零开始的索引;否则为 -1
var emp = employees.FindIndex(e => e.Id ==3);
Console.WriteLine("查到的索引为:" + emp);  //2

FindLast(Predicate) 搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List 中的最后一个匹配元素。

使用方式同FindIndex

IndexOf(T) 搜索指定的对象,并返回整个 List 中第一个匹配项的从零开始的索引

LastIndexOf(T) 搜索指定对象并返回整个 List 中最后一个匹配项的从零开始索引。

List集合类常用操作:四、删除

Clear() 从 List 中移除所有元素。

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") });
Console.WriteLine("Clear清空前" + employees.Count + " " + employees.Capacity);  //Count=5  Capacity = 8
employees.Clear(); //Count=0 
employees.TrimExcess(); //Capacity = 0 
Console.WriteLine("Clear清空后" + employees.Count + " " + employees.Capacity);

Remove(T) 从 List 中移除特定对象的第一个匹配项。

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") });
Console.WriteLine("Remove前" + employees.Count);      //Count = 5      

var emp = employees.FirstOrDefault(e => e.Id == 5);

//如果成功移除了 true,则为 item;否则为 false。 如果在 false 中没有找到 item,则此方法也会返回 List<T>。
if (employees.Remove(emp))
{
    Console.WriteLine("Remove后" + employees.Count);  //Count = 4  
}

RemoveAll(Predicate) 删除与指定谓词所定义的条件匹配的所有元素。

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") });
Console.WriteLine("RemoveAll前" + employees.Count);      //Count = 5      

//遍历list中的元素,将符合lambda表达式结果的元素全部删除掉,返回结果是删除掉的元素个数。
Console.WriteLine("RemoveAll后共删除了" + employees.RemoveAll(e => e.Id >= 3) + "条数据");  //RemoveAll后共删除了3条数据

RemoveAt(Int32) 移除 List 的指定索引处的元素。

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") });
Console.WriteLine("Remove前" + employees.Count);      //Count = 5      

//根据下标删除待删除元素的从零开始的索引。
employees.RemoveAt(4);     //Count = 4 

//查找删除
var index = employees.FindIndex(e => e.Id == 5);
employees.RemoveAt(index);
Console.WriteLine("Remove后" + employees.Count);  //Count = 4  

RemoveRange(Int32, Int32) 从 List 中移除一系列元素。

index Int32 要移除的元素范围的从零开始的起始索引。

count Int32 要移除的元素数。

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") });
Console.WriteLine("RemoveRange前" + employees.Count); //Count = 5 

//根据索引,删除指定条数元素 
var index = employees.FindIndex(e => e.Id == 1); 
employees.RemoveRange(index,3); 
Console.WriteLine("RemoveRange后" + employees.Count); //Count = 2 

//网络素材仅限收藏 方便学习

C# List集合类常用操作

上一篇:解决WinMerge中文乱码的问题


下一篇:redis的安装-windows和linux