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?

Re: Problem with Dictionary<T,U>.Keys enumerator returning invalid keys by Carl

Carl
Mon Jun 05 15:54:51 CDT 2006

"Brian Richards" <brichards@gmail.com> wrote in message
news:uJUkCWNiGHA.4044@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?

What's the type of the key?

If the key is a user defined type that doens't implement equality correctly,
it's possible that you'd get keys back from the enumerator that can't be
found in the dictionary.

-cd



Re: Problem with Dictionary<T,U>.Keys enumerator returning invalid keys by SP

SP
Fri Jun 09 21:09:30 CDT 2006


"Brian Richards" <brichards@gmail.com> wrote in message
news:uJUkCWNiGHA.4044@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

shouldn't that be bool foo = dict.ContainsKey(key);

SP

> }
>
> Does anyone know what kinds of circumstances could cause this?
>
>