By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Problem with Dictionary<T,U>.Keys enumerator returning invalid keys

P: n/a
I'm experiencing some wierd behavior with a Dictionary<T,U> class using
foreach loops, such that the Key returned in the foreach is not contained in
the dictionary.

code:

Dictionary<A, B> dict;
.....
foreach(A key in dict.Keys)
{
bool foo = dict.ContainsKey(A); //returning false
}

Does anyone know what kinds of circumstances could cause this?
Jun 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Brian,

I'm inclined to believe that it doesn't do it, unless you are using the
Dictionary in an improper manner.

For example, are you performing operations on the dictionary from other
threads while enumerating through it?

Can you post a complete example which shows this happening?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Richards" <br*******@gmail.com> wrote in message
news:eN**************@TK2MSFTNGP03.phx.gbl...
I'm experiencing some wierd behavior with a Dictionary<T,U> class using
foreach loops, such that the Key returned in the foreach is not contained
in
the dictionary.

code:

Dictionary<A, B> dict;
....
foreach(A key in dict.Keys)
{
bool foo = dict.ContainsKey(A); //returning false
}

Does anyone know what kinds of circumstances could cause this?

Jun 5 '06 #2

P: n/a
The collection is not being modified in other threads. I would have expected
an Exception were that the case. We're investigating the possibility of it
being a hashing problem. Could it be that if you modify an object used as a
key so that it's hashcode changes you don't get the match with the call to
ContainsKey(...). I don't know if the hashes are always recalculated before
comparing them to the key or if the Dictionary is using a cached version. I
don't suspect that is the case though.

Thanks for your input. We're trying to mockup a reproducible test case.

-Brian

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uk**************@TK2MSFTNGP05.phx.gbl...
Brian,

I'm inclined to believe that it doesn't do it, unless you are using the Dictionary in an improper manner.

For example, are you performing operations on the dictionary from other threads while enumerating through it?

Can you post a complete example which shows this happening?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Richards" <br*******@gmail.com> wrote in message
news:eN**************@TK2MSFTNGP03.phx.gbl...
I'm experiencing some wierd behavior with a Dictionary<T,U> class using
foreach loops, such that the Key returned in the foreach is not contained in
the dictionary.

code:

Dictionary<A, B> dict;
....
foreach(A key in dict.Keys)
{
bool foo = dict.ContainsKey(A); //returning false
}

Does anyone know what kinds of circumstances could cause this?


Jun 5 '06 #3

P: n/a
Brian,

Well, if an object is stored as a key, then GetHashCode is called on it.
Then, the reference is put into a bucket, and then if there is more than one
item in the bucket, it calls Equals on those items.

If you have a custom implementation of GetHashCode (which you should
call if you override Equals), and that implementation is effected by
properties that can be changed, then this could very well be the case. You
might have added a key with a custom implementation of Equals/GetHashCode
and then changed a property on the object which affected the generation of
the key when you wanted to retrieve the item.

If this is the case, this is not an issue with the Dictionary, but what
you are using as the key.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Richards" <br*******@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
The collection is not being modified in other threads. I would have
expected
an Exception were that the case. We're investigating the possibility of it
being a hashing problem. Could it be that if you modify an object used as
a
key so that it's hashcode changes you don't get the match with the call to
ContainsKey(...). I don't know if the hashes are always recalculated
before
comparing them to the key or if the Dictionary is using a cached version.
I
don't suspect that is the case though.

Thanks for your input. We're trying to mockup a reproducible test case.

-Brian

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:uk**************@TK2MSFTNGP05.phx.gbl...
Brian,

I'm inclined to believe that it doesn't do it, unless you are using

the
Dictionary in an improper manner.

For example, are you performing operations on the dictionary from

other
threads while enumerating through it?

Can you post a complete example which shows this happening?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brian Richards" <br*******@gmail.com> wrote in message
news:eN**************@TK2MSFTNGP03.phx.gbl...
> I'm experiencing some wierd behavior with a Dictionary<T,U> class using
> foreach loops, such that the Key returned in the foreach is not contained > in
> the dictionary.
>
> code:
>
> Dictionary<A, B> dict;
> ....
> foreach(A key in dict.Keys)
> {
> bool foo = dict.ContainsKey(A); //returning false
> }
>
> Does anyone know what kinds of circumstances could cause this?
>
>



Jun 5 '06 #4

P: n/a
"Brian Richards" <br*******@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
The collection is not being modified in other threads. I would have
expected
an Exception were that the case. We're investigating the possibility of it
being a hashing problem. Could it be that if you modify an object used as
a
key so that it's hashcode changes you don't get the match with the call to
ContainsKey(...). I don't know if the hashes are always recalculated
before
comparing them to the key or if the Dictionary is using a cached version.
I
don't suspect that is the case though.


To quote from the help on the Dictionary generic class: ""As long as an
object is used as a key in the Dictionary, it must not change in any way
that affects its hash value."

Chris Jobson
Jun 5 '06 #5

P: n/a
"Brian Richards" <br*******@gmail.com> wrote:
I'm experiencing some wierd behavior with a Dictionary<T,U> class using


It would be great if you wouldn't multi-post. Try to post in the one
most-relevant group, or if you can't do that, then cross-post so people
in each group can see answers from other groups.

(I've replied in another group.)

-- Barry

--
http://barrkel.blogspot.com/
Jun 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.