After more digging, here is what the MS documentation says (http://
msdn.microsoft.com/en-us/library/xfhwa508.aspx):
The speed of retrieval depends on the quality of the hashing algorithm
of the type specified for TKey.
As long as an object is used as a key in the Dictionary<TKey, TValue>,
it must not change in any way that affects its hash value. Every key
in a Dictionary<TKey, TValue> must be unique according to the
dictionary's equality comparer. A key cannot be null, but a value can
be, if the value type TValue is a reference type.
This little program illustrates the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDictionary
{
class Program
{
class Variable
{
public int Id { get; set; }
public override int GetHashCode()
{
int toReturn = base.GetHashCode();
toReturn ^= this.Id.GetHashCode();
return toReturn;
}
public override string ToString()
{
return string.Format("Id: {0}", Id);
}
}
static void Main(string[] args)
{
Dictionary<Variable, Variable> dict = new Dictionary<Variable,
Variable>();
List<Variable> list = new List<Variable>();
for(int i = 0; i < 8; i ++)
{
var variable = new Variable();
list.Add(variable);
dict.Add(variable, variable);
variable.Id = i + 1;
}
Variable v = null;
dict.TryGetValue(list[0], out v);
Console.WriteLine("Value=<{0}>",v);
Console.ReadKey();
}
}
}
The key should be immutable, but in the NH case the items added to the
set are not immutable because as soon as they are persisted their ids
change.
I am using NH 3.1.0. I wonder if this is still a problem in NH 3.2.0.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.