在List<T>中,Contains, Exists, Any都可以实现判断元素是否存在。
先上结果。性能方面:Contains 优于 Exists 优于 Any
以下为测试代码
public static void Contains_Exists_Any_Test(int num)
{
List<int> list = new List<int>();
int N = num;
for (int i = 0; i < N; i++)
{
list.Add(i);
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

本文探讨了.NET中List<T>的Contains、Exists和Any三种方法用于检查元素存在的性能差异。测试结果显示,Contains的性能最佳,Exists次之,Any的效率最低。在数据量不大时,推荐使用Exists进行判断。
最低0.47元/天 解锁文章
698

被折叠的 条评论
为什么被折叠?



