473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
5 1764
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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jan-Erik Meyer-Lütgens | last post by:
In the Python Language Reference, I found the following statements about using objects as dictionary keys: 1. "__hash__() should return a 32-bit integer." 2. "The only required property is...
0
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi All, I am having problem returning values from a Stored Procedure that creates a dynamic table (table variable) inserts values during a procedure and then I select from that dynamic table to...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
1
by: Brian Richards | last post by:
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>...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
5
by: Jim Murphy | last post by:
In creating a C# web service, I am having trouble returning a DataTable object as the result of a web method. I have no problem returning native types like string or int, but cannot return a .NET...
3
by: Beorne | last post by:
I have to retrieve the first kei I have inserted in a Dictionary<int,intcalled dict. The only method I found is: Dictionary<int,int>.KeyCollection.Enumerator dke = dict.Keys.GetEnumerator();...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.