473,386 Members | 1,997 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,386 software developers and data experts.

Object identity for Hashtables

Hello,

i have a nasty problem with object identity in
hash tables. I try to use different objects as keys.
It failed bacause i cannot identify object propperly.
Different objects with the same contents have
the same hash code.

DateTime date1 = new DateTime(2001,11,22);
DateTime date2 = new DateTime(2001,11,22);

date1 und date2 have the same hash code. I checked
google and found a thread where a posible solution
for object identity was discussed.

(http://groups.google.de/groups?hl=de...readm=%23xZ2Zw
7pCHA.504%40TK2MSFTNGP12&rnum=1&prev=/groups%3Fhl%3Dde%26ie%3DUTF-8%26oe%3DU
TF-8%26q%3D%2522RuntimeHelpers.GetHashCode%2522%26sa% 3DN%26tab%3Dwg%26meta%3
D)

Unfortenately it just don't work as the method Object.GetHashCode()
does not answer an object identity but a stubid sequence number.

System.Runtime.CompilerServices.RuntimeHelpers.Get HashCode(date1) -> 1
System.Runtime.CompilerServices.RuntimeHelpers.Get HashCode(date2) -> 2
System.Runtime.CompilerServices.RuntimeHelpers.Get HashCode(date1) -> 3

What i need is a method that answers an id that uniquely identifies an
object.
Send to two instances of a class that have the same value must answer
different values. Send two times to the same object must answer the same
value. Is there such a method in .NET?

I added a small sample that shows the problem.

----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Runtime.CompilerServices;

namespace HashtableTest
{
public class IdentityHashcodeProvider : IHashCodeProvider
{
public int GetHashCode(Object obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}

public class IdentityComparer : IComparer
{
public int Compare(Object obj1, Object obj2)
{
if (Object.ReferenceEquals(obj1, obj2)) return 0;
else return 1;
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
DateTime date1 = new DateTime(2001,11,22);
DateTime date2 = new DateTime(2001,11,22);

Console.WriteLine("HashCode for Date 1 : {0}",
RuntimeHelpers.GetHashCode(date1));
Console.WriteLine("HashCode for Date 2 : {0}",
RuntimeHelpers.GetHashCode(date2));
Console.WriteLine("HashCode for Date 1 : {0}",
RuntimeHelpers.GetHashCode(date1));

IdentityHashcodeProvider provider = new
dentityHashcodeProvider();
IdentityComparer comparer = new IdentityComparer();
Hashtable table = new Hashtable(10, provider, comparer);

table.Add(date1, date1.ToString());
if (table.ContainsKey(date2)) Console.WriteLine("No No");
else Console.WriteLine("Ok");

table.Add(date2, date2.ToString());
if (table.ContainsKey(date2)) Console.WriteLine("Ok");
else Console.WriteLine("No No");
}
}
}

----------------------------------------------------------------------------

The other solution discussed there using reflection generates the same
result.
Relacing the statement return RuntimeHelpers.GetHashCode(obj);
with the statments below does not change anything.

System.Reflection.MethodInfo mm;
Type tt = typeof(Object);
mm = tt.GetMethod("GetHashCode");
return (int) mm.Invoke(obj, null);

greetings

Clemens Hoffmann

Jul 19 '05 #1
1 2915
Clemens Hoffmann <ch*******@heeg.de> wrote:
i have a nasty problem with object identity in
hash tables. I try to use different objects as keys.
It failed bacause i cannot identify object propperly.
Different objects with the same contents have
the same hash code.

DateTime date1 = new DateTime(2001,11,22);
DateTime date2 = new DateTime(2001,11,22);
First you need to realise that DateTime is a structure, not a class, so
those are both value type variables. They'll end up being boxed when
you put them in a hashtable. That means if you were using object
identity you would *never* be able to get the value out again in the
normal way, as you'd end up with a different box each time. This is
probably why RuntimeHelpers.GetHashCode was returning different values,
too.
date1 und date2 have the same hash code.
They not only have the same hash code, they are also equal to each
other. The two variables have *exactly* the same value. This is
slightly different from the case where you have two reference type
values which refer to different but equal objects, e.g.

String s1 = "hello";
String s2 = String.Concat ("he", "llo");
What i need is a method that answers an id that uniquely identifies an
object. Send to two instances of a class that have the same value must answer
different values.


Are you interested in instances of classes, or structure values? How
clear are you on the difference between reference types and value
types? It'll be very important when it comes to implementing what you
want to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: midnightswan | last post by:
Hi, I've got a hashtable that contains of list of remote network Player objects. The unique key used is the Player's IP address as a string. The problem I'm getting is when a Player...
1
by: Clemens Hoffmann | last post by:
Hello, i have a nasty problem with object identity in hash tables. I try to use different objects as keys. It failed bacause i cannot identify object propperly. Different objects with the same...
2
by: eastsh | last post by:
I have been looking into using the MS application block for configuration management, and I am very happy with the options it provides. Since it stores hashtables, I decided that for configuration...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: Anders Borum | last post by:
Hello! I have a list of singleton classes (model managers) that store objects internally using hashtables. Each of these classes use a single hashtable to store e.g. users, pages, elements and...
11
by: Amit Agarwal | last post by:
where is the place to attach pricipall object to identity. global file and is it necessary to attach each time user roles to principal object.. amit --- Outgoing mail is certified Virus...
1
by: Curtis | last post by:
Does anyone know the proper method to save information to embedded hashtables. I am trying to save parent/child information to hashtables but I am not getting the correct results I have made a...
11
by: Stephan Keil | last post by:
Hi all, I am a novice with .NET and I am wondering if there is something like an "identity value" of an object. I mean something like the object's address in C++ or C, i.e. a fixed unique value...
7
by: Kamran Shafi | last post by:
Hi, I am creating an arraylist (say masterArrayList) of hashtables, where each hashtable (say table) is of the format key=string, value = arraylist of strings (say existing_strings). In a...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.