【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;
}
相关推荐
-银雾鸢尾-7 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-7 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
心平气和量大福大11 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
白露与泡影12 小时前
Arthas 实战指南:从方法耗时定位到 JVM 变量热修改
服务器·jvm·c#
FrameNotWork14 小时前
HarmonyOS 6.0 LocalStorage页面级存储
华为·交互·harmonyos
FrameNotWork15 小时前
HarmonyOS 6.0 自定义指令与手势组合:从单指到多指的交互进阶
华为·交互·harmonyos
EIP低代码平台16 小时前
EIP低代码平台系统-字典功能讲解
低代码·c#·工作流
TheBestRucy16 小时前
RAG知识库问答系统落地:从向量检索到上下文增强的全链路实践
人工智能·python·langchain·aigc·交互
旧物有情17 小时前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
吉普赛的歌17 小时前
YARP负载均衡配置了多个相同接口导致的报错
c#·yarp