找到最有效的方法,如果一个C#列表中存在的值 [英] Most efficient way to find if a value exists within a C# List

查看:105
本文介绍了找到最有效的方法,如果一个C#列表中存在的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#如果我有bool类型的列表。什么是确定如果列表中包含一个真正的价值最快的方法是什么?我并不需要知道有多少或者真正的价值。我只需要知道如果存在的话。我会寻找很多非常大的列表。

In C# if I have a List of type bool. What is the fastest way to determine if the list contains a true value? I don’t need to know how many or where the true value is. I just need to know if one exists. I will be searching many extremely large lists.

推荐答案

使用或者list.Contains(true)还是list.Any(真)。
。对于一个正常的列表都具有复杂度为O(N)。由于任何()是一个扩展方法,虽然,这需要调用代表,包含()可能仍然是快一点。但可以肯定我会简单地收集了大量的测试两个。

Use either list.Contains(true) or list.Any(true). For a normal list both have complexity O(n). Since Any() is an extension method though, which needs to invoke delegates, the Contains() might still be a bit faster. But to be sure I would simply test both with a large collection.

这篇关于找到最有效的方法,如果一个C#列表中存在的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆