I need to create object cache in memory.
Each object id can be composed from several string, integer and decimal type
values.
ContainsKey does not find existing key in this case.
How to force ContainsKey to compare object contents and find key?
Should i use Binaryformatter to serialize CacheKey, overload CacheKey ==
operator or any other idea ?
Andrus.
To reproduce,
run the code.
Observed:
Key not found
Expected:
Key found
using System.Collections.Generic;
class test {
struct CacheKey {
object Key;
public CacheKey(object key) {
Key = key;
}
}
static Dictionary<CacheKey, objectCache =
new Dictionary<CacheKey, object>();
static void Main() {
CacheKey key = new CacheKey(new object[] { "test1", 1 });
Cache.Add(key, "");
CacheKey key2 = new CacheKey(new object[] { "test1", 1 });
if (!Cache.ContainsKey(key2))
System.Windows.Forms.MessageBox.Show("Key not found");
else
System.Windows.Forms.MessageBox.Show("Key found");
}
}