KeyedCollection中的不必要的空检查< TKey,TItem> .Contains(TKey)

StackOverflow https://stackoverflow.com/questions/1204798

  •  05-07-2019
  •  | 
  •  

刚刚在KeyedCollection.Contains(TKey)中找到了一个不必要的空检查。

欣赏它只是一个非常小的优化,但是不应该认为这种低效率会被自动代码分析工具所吸引?

这是反射器生成的C#:

public bool Contains(TKey key)
{
    if (key == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
    }
    if (this.dict != null)
    {
        return this.dict.ContainsKey(key);
    }
    if (key != null) // Not needed as key cannot be null
    {
        foreach (TItem local in base.Items)
        {
            if (this.comparer.Equals(this.GetKeyForItem(local), key))
            {
                return true;
            }
        }
    }
    return false;
}

另外,发送补丁的最佳方式是什么? ;-)通过 .net论坛或?

有帮助吗?

解决方案

无论如何,这可能会被JIT优化,所以你真的不需要担心它。无论如何,空检查的成本接近于零。

要报告错误,您可以使用Microsoft Connect网站。但我不认为他们会解决它......

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top