32
I'm investigating an exception that a colleague just got while running an application through Visual Studio 2010:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=mscorlib
StackTrace:
at System.Collections.Generic.GenericEqualityComparer`1.Equals(T x, T y)
at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value)
at xxxxxxx.xxxxxxx.xxxxxxx.RepositoryBase`2.GetFromCache(TIdentity id)
Using .NET Reflector, I have looked at the code for GenericEqualityComparer<T>.Equals(T x, T y), and I can't see any possible cause for a NullReferenceException.
//GenericEqualityComparer<T>.Equals(T x, T y) from mscorlib 4.0.30319.269
public override bool Equals(T x, T y)
{
if (x != null)
{
return ((y != null) && x.Equals(y));
}
if (y != null)
{
return false;
}
return true;
}
The type of T, TKey and TIdentity are all the same type in this stack trace.
The type is a custom type called Identity that implements IEquatable<Identity>. It is immutable and cannot be constructed with null values for the fields that it uses in its implementation of Equals(Identity other). It also overrides Equals(object obj) like this:
public override bool Equals(object obj)
{
if ((object)this == obj)
{
return true;
}
return Equals(obj as Identity);
}
public bool Equals(Identity other)
{
if ((object)this == (object)other)
{
return true;
}
if ((object)other == null