C# 中操作集合的方法

  1. Add:向集合中添加元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3 };
    numbers.Add(4);
    // numbers 现在为 { 1, 2, 3, 4 }
  2. Remove:从集合中移除指定的元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    numbers.Remove(3);
    // numbers 现在为 { 1, 2, 4 }
  3. Contains:检查集合中是否包含指定的元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    bool containsThree = numbers.Contains(3);
    // containsThree 为 true
  4. Clear:从集合中移除所有元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    numbers.Clear();
    // numbers 现在为空集合 {}
  5. Count:获取集合中元素的数量。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int count = numbers.Count;
    // count 现在为 4
  6. Sort:对集合进行排序。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 3, 2, 4, 1 };
    numbers.Sort();
    // numbers 现在为 { 1, 2, 3, 4 }
  7. Reverse:反转集合中元素的顺序。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    numbers.Reverse();
    // numbers 现在为 { 4, 3, 2, 1 }
  8. Find:查找符合指定条件的第一个元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int evenNumber = numbers.Find(x => x % 2 == 0);
    // evenNumber 现在为 2
  9. FindAll:查找符合指定条件的所有元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    List<int> evenNumbers = numbers.FindAll(x => x % 2 == 0);
    // evenNumbers 现在为 { 2, 4 }
  10. FindIndex:查找符合指定条件的第一个元素的索引。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int index = numbers.FindIndex(x => x % 2 == 0);
    // index 现在为 1(等于2的索引)
  11. FindLast:查找符合指定条件的最后一个元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4, 2 };
    int lastNumber = numbers.FindLast(x => x == 2);
    // lastNumber 现在为 2
  12. FindLastIndex:查找符合指定条件的最后一个元素的索引。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4, 2 };
    int lastIndex = numbers.FindLastIndex(x => x == 2);
    // lastIndex 现在为 4(等于2的最后一个索引)
  13. TrueForAll:判断集合中的所有元素是否都满足指定条件。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    bool allEven = numbers.TrueForAll(x => x % 2 == 0);
    // allEven 为 false
  14. Exists:判断集合中是否存在满足指定条件的元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    bool evenExists = numbers.Exists(x => x % 2 == 0);
    // evenExists 为 true
  15. Distinct:从集合中排除重复的元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 2, 3, 3, 4 };
    List<int> distinctNumbers = numbers.Distinct().ToList();
    // distinctNumbers 现在为 { 1, 2, 3, 4 }
  16. Union:合并两个集合,生成一个包含两个集合中唯一元素的新集合。

    csharp 复制代码
    List<int> numbers1 = new List<int>(){ 1, 2, 3 };
    List<int> numbers2 = new List<int>(){ 3, 4, 5 };
    List<int> uniqueNumbers = numbers1.Union(numbers2).ToList();
    // uniqueNumbers 现在为 { 1, 2, 3, 4, 5 }
  17. Intersect:获取两个集合中共有的元素。

    csharp 复制代码
    List<int> numbers1 = new List<int>(){ 1, 2, 3 };
    List<int> numbers2 = new List<int>(){ 3, 4, 5 };
    List<int> commonNumbers = numbers1.Intersect(numbers2).ToList();
    // commonNumbers 现在为 { 3 }
  18. Except:从第一个集合中移除在第二个集合中存在的元素。

    csharp 复制代码
    List<int> numbers1 = new List<int>(){ 1, 2, 3, 4 };
    List<int> numbers2 = new List<int>(){ 3, 4, 5 };
    List<int> remainingNumbers = numbers1.Except(numbers2).ToList();
    // remainingNumbers 现在为 { 1, 2 }
  19. Concat:将两个集合连接为一个新的集合。

    csharp 复制代码
    List<int> numbers1 = new List<int>(){ 1, 2, 3 };
    List<int> numbers2 = new List<int>(){ 4, 5 };
    List<int> combinedNumbers = numbers1.Concat(numbers2).ToList();
    // combinedNumbers 现在为 { 1, 2, 3, 4, 5 }
  20. Aggregate:使用指定的函数将集合中的元素聚合为一个值。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int sum = numbers.Aggregate((x, y) => x + y);
    // sum 现在为 10
  21. Any:判断集合中是否存在满足指定条件的元素。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    bool anyEven = numbers.Any(x => x % 2 == 0);
    // anyEven 为 true
  22. All:判断集合中的所有元素是否都满足指定条件。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    bool allEven = numbers.All(x => x % 2 == 0);
    // allEven 为 false
  23. Min:获取集合中的最小值。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int min = numbers.Min();
    // min 现在为 1
  24. Max:获取集合中的最大值。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int max = numbers.Max();
    // max 现在为 4
  25. Average:计算集合中元素的平均值。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    double average = numbers.Average();
    // average 现在为 2.5
  26. Sum:计算集合中元素的总和。

    csharp 复制代码
    List<int> numbers = new List<int>(){ 1, 2, 3, 4 };
    int sum = numbers.Sum();
    // sum 现在为 10

请注意,示例中使用的集合类型为List<int>,其他数据类型和集合类型也可以按照类似的方式使用这些操作方法。

相关推荐
铭哥的编程日记1 小时前
《C++ list 完全指南:从基础到高效使用》
开发语言·c++·list
Humbunklung5 小时前
C# WPF 实现读取文件夹中的PDF并显示其页数
pdf·c#·wpf·npoi·gemini·itext
时光追逐者6 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 48 期(2025年7.21-7.27)
c#·.net·.netcore·.net core
蓝点lilac6 小时前
C# 调用邮箱应用发送带附件的邮件
c#·.net
工程师0077 小时前
C#多线程,同步与异步详解
开发语言·c#·多线程·同步·异步编程
小乖兽技术9 小时前
在 .NET 中使用 Base64 时容易踩的坑总结
开发语言·c#·.net
kyle~10 小时前
C++---初始化列表(initializer_list)
java·c++·list
小乖兽技术13 小时前
C#与C++交互开发系列(二十六):构建跨语言共享缓存,实现键值对读写与数据同步(实践方案)
c++·c#·交互
张人玉15 小时前
c#Lambda 表达式与事件核心知识点整理
开发语言·python·c#
SkyrimCitadelValinor1 天前
c#中让图片显示清晰
开发语言·c#