tags:

views:

65

answers:

1

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 ?

+2  A: 

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...

Thomas Levesque
also it is going to be branch predicted incredibly well on modern cpu's so in real terms it is likely to actually make next to no difference even if not jitted away...
ShuggyCoUk
If can be JIT'd away, then shouldn't a code analysis tool be able to also pick it up?
Si
Yes, probably... what's your point ?
Thomas Levesque
Because that was half of the question :)
Si