473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2995
I created an IdentityCompare r like this:

public class IdentityCompare r : IEqualityCompar er<TDataRecord>
{
public bool Equals(TDataRec ord x, TDataRecord y)
{
return Object.Referenc eEquals(x, y);
}

public int GetHashCode(TDa taRecord 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**********@gm ail.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 EqualityCompare r delegate in the constructor, use
object.Referenc eEquals as the EqualityCompare r.
>
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**********@g mail.comwrote in message
news:c5******** *************** ***********@25g 2000prz.googleg roups.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...@g mail.com" <jehugalea...@g mail.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.GetHashC ode() 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.Collecti ons.Generic;

public class IdentityCompare r<T: IEqualityCompar er<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
4906
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 that objects which compare equal have the same hash value." 3. "If a class does not define a __cmp__() method it should not define a __hash__() operation either."
6
1983
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
3598
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
4
3206
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 Object2 by using == which works but shouldn't it be using .Equals()? e.g. Object1.Equals(Object2) I believe == should be using for simple type evaluation e.g. int a = 0;
90
10775
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 future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
12
3165
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, letters to numbers, sort of a table for lookup and a table for reverse lookup. I thought that a dictionary would be the structure to use- write it once and in a second instance swap the keys for values and values for keys. However, reversing a...
37
2799
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 behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based. This means that x == y will never raise an exception, as is the situation is now. Here's his reason:
3
3676
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
1591
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: column1 - 10 column2 - 12
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.