【问题标题】:Easiest way to check if value is one of a set of values in C#?检查值是否是 C# 中的一组值之一的最简单方法?
【发布时间】:2014-03-12 01:15:59
【问题描述】:

检查一个值是否是一组值中的一个的最简单方法是什么?

例如。

if (new List<CustomerType>{CustomerType.Overseas, CustomerType.Interstate}.Contains(customerType)) 
{
    // code here
}

【问题讨论】:

  • 您可以考虑HashSet 而不是List,但是您的样本有什么问题?
  • @GrantWinney:有没有更短的方法?
  • @GrantWinney 是的,有一种更快的方法。 HashSet 是一种更快的包含集合。
  • @Blam 在验证了我的时间后List 比我最初测量的要慢得多,所以List 几乎无法击败/匹配HashSet 0-2 个项目(代码发布在下面) -所以HashSet 是更好的选择。像往常一样 - 永远不要猜测性能数字......感谢您的乐趣。
  • @GrantWinney List 和 HashSet 都包含不是 LINQ 扩展的方法。他们在不使用 Systems.linq 的情况下工作;

标签: c# .net list enums set


【解决方案1】:

为什么要创建一个列表?
为什么每次都要创建?

HashSet 是最快的包含。

private HashSet<CustomerType> CustomerTypes = new HashSet<CustomerType>() {CustomerType.Overseas, CustomerType.Interstate};
if (CustomerTypes.Contains(customerType))
{ }

这已经有更多的讨论了。
考虑速度。
如果您只打算评估一次(或内联),那么这将获胜

if (customerType == CustomerType.Overseas || customerType == CustomerType.Interstate) 
{
    // code here
}

如果您要进行多次评估,那么 HashSet 将获胜。
在应用程序启动时创建一次 HashSet。
不要每次都创建 HashSet(或 List 或 Array)。

对于少量的列表或数组可能会获胜,但包含是 O(n),因此响应会随着列表越长而降低。

HashSet.Contains 为 O(1),因此响应不会随着 n 的增大而降低。

【讨论】:

  • +1。考虑从答案中删除问题并将其转换为陈述......
  • @AlexeiLevenkov 使用HashSet和代码示例的哪一部分不是一个明确的答案声明?
  • 我今天非常不清楚......我的意思是:“你为什么要创建一个列表?” ->“使用 HashSet,因为它是为此类操作明确设计的......”。 “你为什么每次都要创造它?” -> “除非你需要在一个地方/一次进行比较,否则最好……”。
【解决方案2】:

你可以循环它,看看它是否有效。制作一个你想要的东西的数组,然后使用 for 循环并比较它们。

CustomerType[] customers = {new CustomerType("Overseas"), new CustomerType("Interstate")};
public CustomerType(int id, String type){
this.type = type;
}
public String getType(){return type;}

然后你可以扫描类型

for(int i = 0; i < CustomerTypes.customers.length; i++){
if(CustomerTypes.customers[i].getType == "Overseas"){
//CODE
}
else if(CustomerTypes.customers[i].getType == "Interstate"){
//CODE
}else{
//CODE "UNKNOWN CUSTOMER TYPE"
}
}

我很确定这是您的要求。但如果不是,请澄清,我很乐意提供更多帮助!

【讨论】:

    【解决方案3】:

    不是答案:HashSetList 的比较Contains - TL;DR;更喜欢 - HashSet

    猜测:List 更简单,因此对于少量项目,它应该比HashSet 更快,因此使用new List&lt;EnumType&gt;{EnumType.Value1, EnumType.Value2} 与使用HashSet 的相同代码一样好。

    现实:ListContains 比预期慢得多(与直接迭代 Array 相比,后者击败 HashSet 最多 10 个项目),所以 List 可以击败 HashSet项目数为 0-2 或 List 中的首选比非常少(

    测量:

        static void ComapreListAndHashSet()
        {
            bool found = false;
            var length = 10000000;
            const int searchValue = -1;
    
            for (int size = 0; size < 15; size++)
            {
                var list = Enumerable.Range(1, size).ToList();
                var hashSet = new HashSet<int>(list);
    
                var timeForList = new Stopwatch();
                timeForList.Start();
                for (int i = 0; i < length; i++)
                {
                    found = found & (list.Contains(searchValue));
                }
                timeForList.Stop();
    
                var timeForHash = new Stopwatch();
                timeForHash.Start();
                for (int i = 0; i < length; i++)
                {
                    found = found & hashSet.Contains(searchValue);
                }
                timeForHash.Stop();
                Console.WriteLine("{0},\t{1},\t{2}", hashSet.Count,
                    timeForList.ElapsedMilliseconds, timeForHash.ElapsedMilliseconds);
            }
        }
    

    输出:

    Size:   List:   HashSet(ms):
    0,      120,    155
    1,      163,    194
    2,      203,    189
    3,      250,    189
    4,      294,    188
    5,      332,    188
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-04
      • 2010-09-23
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      相关资源
      最近更新 更多