【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;
}
相关推荐
Lost of 程序猿8 小时前
用 Quartz.NET 优雅地处理 .NET 定时任务:从选型到 Docker 部署全记录
后端·c#·asp.net
林川~0118 小时前
Unity 万能物理检测工具:射线检测 / 范围检测 / 层级过滤 / 编辑器可视化(可直接拿去用)
游戏·unity·c#·射线检测·通用工具
He BianGu1 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
tang_04271 天前
【Hi.Ltd 专题】第9期:Interop 配置互操作(JSON/INI/XML/YAML/注册表/扫码)
经验分享·c#·hi.ltd系列
yi碗汤园1 天前
MQTT消息队列遥测传输协议
网络·网络协议·unity
tang_04271 天前
【Hi.Ltd 专题】第7期:Threading 采集线程、锁与 LRU 缓存
经验分享·c#·hi.ltd系列
A_nanda1 天前
C# 界面卡顿:从定位到根治
c#
2501_930707781 天前
使用C#代码为新创建的 Word 文档创建目录
c#·word
林川~011 天前
Unity 常用 API 与核心类全解:从生命周期到协程,附性能避坑清单(Unity 6 适配)
unity·游戏引擎·常用api
czhc11400756631 天前
同一个数,两把尺子:六组“看着一样、其实不一样“
c#