473,412 Members | 2,142 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,412 software developers and data experts.

Force Dictionary to use Identity versus Equality

Hello:

Is there a way to force a Dictionary to determine equivilence by
memory location?

I have a generic class where I don't want to treat equal items as
identical items. Only when two references point to the same instance
do I want to treat them as the same dictionary key.

Any help?

Thanks,
Travis
Aug 28 '08 #1
5 2985
I created an IdentityComparer like this:

public class IdentityComparer : IEqualityComparer<TDataRecord>
{
public bool Equals(TDataRecord x, TDataRecord y)
{
return Object.ReferenceEquals(x, y);
}

public int GetHashCode(TDataRecord obj)
{
return obj.GetHashCode();
}
}

I pass this to the constructor of the Dictionary. Is there a way to
improve the GetHashCode, however?
Aug 28 '08 #2
je**********@gmail.com wrote:
Hello:

Is there a way to force a Dictionary to determine equivilence by
memory location?

I have a generic class where I don't want to treat equal items as
identical items. Only when two references point to the same instance
do I want to treat them as the same dictionary key.

Any help?
Dictionary accepts an EqualityComparer delegate in the constructor, use
object.ReferenceEquals as the EqualityComparer.
>
Thanks,
Travis

Aug 28 '08 #3
Thanks. Any ideas about improving the GetHashCode?

I'm assuming there isn't anything I can do about the GetHashCode. I
can't use memory addresses since they are volitile because of the
memory management.

Thanks again,
Travis
Aug 28 '08 #4


<je**********@gmail.comwrote in message
news:c5**********************************@25g2000p rz.googlegroups.com...
Thanks. Any ideas about improving the GetHashCode?

I'm assuming there isn't anything I can do about the GetHashCode. I
can't use memory addresses since they are volitile because of the
memory management.
Sorry, I don't know of any particular solution for that. But hash code
collisions should be pretty minimal unless the number of objects which are
equal-in-value-but-not-reference is a lot higher than you suggested.
>
Thanks again,
Travis
Aug 28 '08 #5
On Aug 29, 12:00*am, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
Thanks. Any ideas about improving the GetHashCode?

I'm assuming there isn't anything I can do about the GetHashCode. I
can't use memory addresses since they are volitile because of the
memory management.
There isn't anything that can be done in pure C#. However, it is
possible in IL to make non-virtual method calls even for virtual
methods, explicitly specifying the type that should provide the
implementation that is called. In your case, what you need is to make
a non-virtual call to Object.GetHashCode() on your objects - that
would be the default implementation that provides identity-based hash.
So, you take the C# code written along these lines:

using System;
using System.Collections.Generic;

public class IdentityComparer<T: IEqualityComparer<T>
where T : class
{
public bool Equals(T x, T y)
{
return x == y;
}

public int GetHashCode(T x)
{
return x.GetHashCode();
}
}

Compile it, then ildasm it. In the resulting .il file, find this
instruction:

callvirt instance int32 [mscorlib]System.Object::GetHashCode()

replace "callvirt" with "call", and use ilasm to recompile the
assembly. Now you've got your identity comparer with proper
GetHashCode().
Aug 29 '08 #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...
6
by: Marius Bernklev | last post by:
In python, it seems like immutable objects are equal under both equality and identity: >>> 5 == 5 True >>> 5 is 5 True >>> "hei" == "hei" True >>> "hei" is "hei"
57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
4
by: Chris | last post by:
This is something that has been bugging me for some time. When exactly should I be using == instead of .Equals() ? I see a lot of code that performs object evaluation e.g. Is Object1 the same as...
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...
12
by: rudysanford | last post by:
I just started messing with programming and started with Python. Part of my first project deals with translating numerical values to letters. I would like to be able to do the reverse as well,...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
3
by: kim.nolsoee | last post by:
Hi I want to use the Dictionary Classs that will load my own class called KeyClass used as TKey. Here is the code: public class Dictionary { public static void Main()
2
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have a Dictionary (that is a property for a class) like this: public Dictionary<string, decimal?Value = new Dictionary<string, decimal?>(); where I put the values like these: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.