Question

Just found an unnecessary null check in KeyedCollection.Contains(TKey).

Appreciate it's only a very small optimization, but shouldn't thought this sort of inefficiency be picked up by an automated code analysis tool?

Here's the C# generated by reflector:

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;
}

Also, what's the best way of sending in a patch? ;-) Through the .net forums or ?

Was it helpful?

Solution

This will probably be optimized by the JIT anyway, so you don't really need to worry about it. Anyway, the cost of a null check is close to zero.

To report a bug, you can use the Microsoft Connect website. But I don't think they will fix it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top