【Unity通用工具类】列表扩展方法ListExtensions

  • 判断集合是否为 null 或不包含任何元素
csharp 复制代码
/// <summary>
/// 判断集合是否为 null 或不包含任何元素
/// 无需遍历整个集合即可获取元素数量
/// 使用LINQ's Any()方法判断集合是否为空
/// 会产生一些垃圾回收开销
/// </summary>
/// <param name="list">List to evaluate</param>
public static bool IsNullOrEmpty<T>(this IList<T> list) {
	return list == null || !list.Any();
}
  • 复制列表
csharp 复制代码
/// <summary>
/// 创建一个新的列表,它是原始列表的副本
/// </summary>
/// <param name="list">需要复制的原始列表</param>
/// <returns>返回复制列表</returns>
public static List<T> Clone<T>(this IList<T> list) {
	List<T> newList = new List<T>();
	foreach (T item in list) {
		newList.Add(item);
	}
	return newList;
}
  • 更新列表内容
csharp 复制代码
/// <summary>
/// 更新列表内容
/// </summary>
/// <param name="list"></param>
/// <param name="items"></param>
/// <typeparam name="T"></typeparam>
public static void RefreshWith<T>(this List<T> list, IEnumerable<T> items)
{
	list.Clear();
	list.AddRange(items);
}
  • 交换元素
csharp 复制代码
/// <summary>
/// 交换列表中指定索引处的两个元素。
/// </summary>
/// <param name="list">列表</param>
/// <param name="indexA">第一个元素的索引</param>
/// <param name="indexB">第二个元素的索引</param>
public static void Swap<T>(this IList<T> list, int indexA, int indexB) {
	(list[indexA], list[indexB]) = (list[indexB], list[indexA]);
}
  • 打乱列表
csharp 复制代码
/// <summary>
/// 使用 Fisher-Yates 算法的 Durstenfeld 实现方法对列表中的元素进行随机排序
/// 此方法会就地修改输入列表,确保每个排列出现的概率均等,并返回修改后的列表,以便进行方法链式调用
/// 参考: http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
/// </summary>
/// <param name="list">需要打乱顺序的列表</param>
/// <typeparam name="T">列表中元素的类型</typeparam>
/// <returns>打乱顺序后的列表</returns>
public static IList<T> Shuffle<T>(this IList<T> list) {
	Random rng = new Random();
	int count = list.Count;
	while (count > 1) {
	--count;
	int index = rng.Next(count + 1);
	(list[index], list[count]) = (list[count], list[index]);
	}
	return list;
}
  • 过滤集合
csharp 复制代码
/// <summary>
/// Filters a collection based on a predicate and returns a new list根据条件过滤集合并返回一个新的列表。
/// containing the elements that match the specified condition.包含符合指定条件的元素。
/// </summary>
/// <param name="source">The collection to filter.需要过滤的集合</param>
/// <param name="predicate">The condition that each element is tested against.用于测试每个元素的条件。</param>
/// <returns>A new list containing elements that satisfy the predicate.一个包含满足条件的元素的新列表。</returns>
public static IList<T> Filter<T>(this IList<T> source, Predicate<T> predicate) {
	List<T> list = new List<T>();
	foreach (T item in source) {
		if (predicate(item)) {
			list.Add(item);
		}
	}
	return list;
}
相关推荐
郝学胜-神的一滴1 小时前
[简化版 GAMES 104] 现代游戏引擎 05:游戏引擎世界构建核心机制深度解析
c++·程序人生·unity·游戏引擎·计算机图形学·opengl
CDN3602 小时前
俄语区跨境站点优化实战:CDN+Nginx 解决 H5 白屏、交互延迟、链路卡顿问题
运维·nginx·交互·h5 加速·nginx 轻交互调优
椒颜皮皮虾྅14 小时前
OpenVINO C# API 3.3.1:支持OpenVINO 2026.3及GenAI增强
人工智能·c#·openvino
半亩码田16 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
唐青枫1 天前
别再手写 args[0]:C#.NET CommandLineParser 命令行工具实战详解
c#·.net
LccKyI1 天前
C#学习day06(方法/函数及其关键字,附思维导图)
经验分享·学习·c#
.Peter1 天前
.NET/WPF 程序在部分 Windows 电脑无法保存用户环境变量:使用 DPAPI 实现安全兼容
windows·信息安全·c#·.net·wpf·环境变量·dpapi
LONGZETECH1 天前
无人机实训高成本痛点解法:虚拟仿真实现 70% 耗材损耗下降
大数据·算法·unity·架构·无人机
农村小镇哥1 天前
怎么把HTM格式转化成WORD
ui·c#·word
农村小镇哥1 天前
C#读取CSV文件的方法
服务器·数据库·c#