【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;
}
相关推荐
Porco.w10 分钟前
C#与三菱PLC FX5U通信
网络·c#
方见华Richard2 小时前
方见华个人履历|中英双语版
人工智能·经验分享·交互·原型模式·空间计算
E_ICEBLUE2 小时前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
JQLvopkk4 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
微祎_4 小时前
Flutter for OpenHarmony:构建一个 Flutter 镜像绘图游戏,对称性认知、空间推理与生成式交互设计
flutter·游戏·交互
蓝帆傲亦5 小时前
前端性能极速优化完全指南:从加载秒开体验到丝滑交互
前端·交互
繁星流动 >_<6 小时前
Axure-局部放大图片交互
交互·axure·photoshop
在路上看风景6 小时前
31. Unity 异步加载的底层细节
unity
wxin_VXbishe6 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
天人合一peng7 小时前
Unity中做表头时像work中整个调整宽窄
unity