[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Is there an equivalent of java's System.identityHashCode()?

Taco H. in den Bosch

11/25/2002 11:20:00 AM

I urgently need an equivalent of java's java.lang.System.identityHashCode().
I have searched the framework a number of times, but I am unable to find it,
and implementing a replacement is not possible.

http://java.sun.com/j2se/1.3/docs/api/java/lang/S... has more details
on the method.

Sorry for the crossposting, but this is the third time I'm asking. I got no
response before, that is why I am crossposting the question now.

Thanks in advance,

Taco.


20 Answers

Jason Sobell - iGadget

11/25/2002 1:06:00 PM

0

Yes, use .GetHashCode() on any object. It doesn't show up in intellisense,
but it is there :)

Cheers,
Jason

"Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote in message
news:#YFByvGlCHA.2276@tkmsftngp10...
> I urgently need an equivalent of java's
java.lang.System.identityHashCode().
> I have searched the framework a number of times, but I am unable to find
it,
> and implementing a replacement is not possible.
>
> http://java.sun.com/j2se/1.3/docs/api/java/lang/S... has more
details
> on the method.
>
> Sorry for the crossposting, but this is the third time I'm asking. I got
no
> response before, that is why I am crossposting the question now.
>
> Thanks in advance,
>
> Taco.
>
>


Taco H. in den Bosch

11/25/2002 1:25:00 PM

0

Dear Jason,

No, that won't work: GetHashCode() can be overridden by a derived class,
just like hashCode can be overridden in java. The point of identityHashCode
is that it returns the same value as hashCode would if
it weren't overridden.

Taco.

"Jason Sobell - iGadget" <iGadget_@hotmail.com> wrote in message
news:#UhcBsHlCHA.3748@tkmsftngp08...
> Yes, use .GetHashCode() on any object. It doesn't show up in
intellisense,
> but it is there :)
>
> Cheers,
> Jason
>
> "Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote in message
> news:#YFByvGlCHA.2276@tkmsftngp10...
> > I urgently need an equivalent of java's
> java.lang.System.identityHashCode().
> > I have searched the framework a number of times, but I am unable to find
> it,
> > and implementing a replacement is not possible.
> >
> > http://java.sun.com/j2se/1.3/docs/api/java/lang/S... has more
> details
> > on the method.
> >
> > Sorry for the crossposting, but this is the third time I'm asking. I got
> no
> > response before, that is why I am crossposting the question now.
> >
> > Thanks in advance,
> >
> > Taco.
> >
> >
>
>


Paul

11/25/2002 9:36:00 PM

0

I think the point of GetHashCode() is so when you store
the object in a Hashtable, the Hashtable could determine
which bucket to put the object in so it can quickly find
it again.

The trick to designing GetHashCode() is to have a good
distribution of objects in the buckets of the Hashtable.

Have a look at Object.GetHashCode() on MSDN.

Cheers,
Paul

>-----Original Message-----
>Dear Jason,
>
>No, that won't work: GetHashCode() can be overridden by a
derived class,
>just like hashCode can be overridden in java. The point
of identityHashCode
>is that it returns the same value as hashCode would if
>it weren't overridden.
>
>Taco.
>
>"Jason Sobell - iGadget" <iGadget_@hotmail.com> wrote in
message
>news:#UhcBsHlCHA.3748@tkmsftngp08...
>> Yes, use .GetHashCode() on any object. It doesn't show
up in
>intellisense,
>> but it is there :)
>>
>> Cheers,
>> Jason
>>
>> "Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote
in message
>> news:#YFByvGlCHA.2276@tkmsftngp10...
>> > I urgently need an equivalent of java's
>> java.lang.System.identityHashCode().
>> > I have searched the framework a number of times, but
I am unable to find
>> it,
>> > and implementing a replacement is not possible.
>> >
>> >
http://java.sun.com/j2se/1.3/docs/api/java/lang/S...
has more
>> details
>> > on the method.
>> >
>> > Sorry for the crossposting, but this is the third
time I'm asking. I got
>> no
>> > response before, that is why I am crossposting the
question now.
>> >
>> > Thanks in advance,
>> >
>> > Taco.
>> >
>> >
>>
>>
>
>
>.
>

Mattias Sjögren

11/25/2002 10:00:00 PM

0

Taco,

>I urgently need an equivalent of java's java.lang.System.identityHashCode().
>I have searched the framework a number of times, but I am unable to find it,
>and implementing a replacement is not possible.

Have a look at the IdentityHashCodeProvider sample at

http://www.msjogren.n...eng/sample...

It contains IHashCodeProvider implementations that does this written
in C++ and IL Asm (the two MSFT languages that supports making
non-virtual calls).



Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.n...
Please reply only to the newsgroup.

Dario Russi [MS]

11/25/2002 11:11:00 PM

0

System.Runtime.CompilerServices.RuntimeHelper.GetHashCode(Object o);
is a helper that will always call Object.GetHashCode(), even when overriden.
Of a course a language that let you do non virtual call would be enough and
you wouldn't need the helper.
C++ will allow you that, but not C#.

HTH
dario

"Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote in message
news:eo3np1HlCHA.716@tkmsftngp11...
> Dear Jason,
>
> No, that won't work: GetHashCode() can be overridden by a derived class,
> just like hashCode can be overridden in java. The point of
identityHashCode
> is that it returns the same value as hashCode would if
> it weren't overridden.
>
> Taco.
>
> "Jason Sobell - iGadget" <iGadget_@hotmail.com> wrote in message
> news:#UhcBsHlCHA.3748@tkmsftngp08...
> > Yes, use .GetHashCode() on any object. It doesn't show up in
> intellisense,
> > but it is there :)
> >
> > Cheers,
> > Jason
> >
> > "Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote in message
> > news:#YFByvGlCHA.2276@tkmsftngp10...
> > > I urgently need an equivalent of java's
> > java.lang.System.identityHashCode().
> > > I have searched the framework a number of times, but I am unable to
find
> > it,
> > > and implementing a replacement is not possible.
> > >
> > > http://java.sun.com/j2se/1.3/docs/api/java/lang/S... has more
> > details
> > > on the method.
> > >
> > > Sorry for the crossposting, but this is the third time I'm asking. I
got
> > no
> > > response before, that is why I am crossposting the question now.
> > >
> > > Thanks in advance,
> > >
> > > Taco.
> > >
> > >
> >
> >
>
>


Mike Schilling

11/26/2002 1:29:00 AM

0

"Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote in message
news:#YFByvGlCHA.2276@tkmsftngp10...
> I urgently need an equivalent of java's
java.lang.System.identityHashCode().
> I have searched the framework a number of times, but I am unable to find
it,
> and implementing a replacement is not possible.
>
> http://java.sun.com/j2se/1.3/docs/api/java/lang/S... has more
details
> on the method.

This works:

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

That is, if you call Object.GetHashCode() reflectively, the call is treated
as non-virtual. I have been unable to get official word from Microsoft on
why this is true and whether it's a bug or a feature.


Chad Myers

11/26/2002 4:55:00 AM

0

I'd vote for feature.

If you wanted to call the GetHashCode on obj's type rather
than System.Object, you'd get the MethodInfo from obj's
type.

-c

"Mike Schilling" <mscottschilling@hotmail.com> wrote in message
news:ewPnoLOlCHA.2256@tkmsftngp12...
> "Taco H. in den Bosch" <t.indenbosch@scansys.nl> wrote in message
> news:#YFByvGlCHA.2276@tkmsftngp10...
> > I urgently need an equivalent of java's
> java.lang.System.identityHashCode().
> > I have searched the framework a number of times, but I am unable to
find
> it,
> > and implementing a replacement is not possible.
> >
> > http://java.sun.com/j2se/1.3/docs/api/java/lang/S... has more
> details
> > on the method.
>
> This works:
>
> System.Reflection.MethodInfo mm;
> int hCode;
> Type tt = typeof(Object);
> mm = tt.GetMethod("GetHashCode");
> hCode = (int) mm.Invoke(obj, null);
>
> That is, if you call Object.GetHashCode() reflectively, the call is
treated
> as non-virtual. I have been unable to get official word from
Microsoft on
> why this is true and whether it's a bug or a feature.
>
>


Jon Skeet

11/26/2002 8:08:00 AM

0

cmyers@N0.SP.4M.austin.rr.com wrote:
> I'd vote for feature.
>
> If you wanted to call the GetHashCode on obj's type rather
> than System.Object, you'd get the MethodInfo from obj's
> type.

Seems pretty horrible to me - I'd much rather it acted virtually.

One real-world situation where you might want it to behave that way is if
you have several plugin classes all deriving from the same base class (or
implementing the same interface). You want to call the same method on
each of them. Rather than having to get the method reflectively for each
individual object, it would be easier if you could get the method from
base class once, and invoke it with a different target each time.

More fundamentally though, I just don't like this kind of behaviour in
terms of encapsulation. It feels like it's going behind the back of the
object in question. Suppose the object has deliberately overridden the
method in order to do housekeeping and keep its internal state consistent
- calling the base class's method could easily break that.

(And yes, I know you can do it in C++. I don't like that either, and
think it was a good idea to get rid of it from Java and C#.)

--
Jon Skeet
skeet@pobox.com - http://www.pobox....
If replying to the group, please do not mail me at the same time

Chad Myers

11/26/2002 6:52:00 PM

0


"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.184d2e62ce6717129897fb@10.1.1.14...
> cmyers@N0.SP.4M.austin.rr.com wrote:
> > I'd vote for feature.
> >
> > If you wanted to call the GetHashCode on obj's type rather
> > than System.Object, you'd get the MethodInfo from obj's
> > type.
>
> Seems pretty horrible to me - I'd much rather it acted virtually.
>
> One real-world situation where you might want it to behave that way is
if
> you have several plugin classes all deriving from the same base class
(or
> implementing the same interface). You want to call the same method on
> each of them. Rather than having to get the method reflectively for
each
> individual object, it would be easier if you could get the method from
> base class once, and invoke it with a different target each time.

It's not any harder to get the method for each type.

object myPlugin = Activator.CreateIns.... blah blah blah

myPlugin.GetType().GetMethod("Foo").Invoke(...);

> More fundamentally though, I just don't like this kind of behaviour in
> terms of encapsulation. It feels like it's going behind the back of
the
> object in question. Suppose the object has deliberately overridden the
> method in order to do housekeeping and keep its internal state
consistent
> - calling the base class's method could easily break that.

Keep in mind, this is reflection which basically breaks all
encapsulation :)

You should always take care to get the type of the object you're
working with.

Only in special cases where you need to call a method non-virtually
should you use a base type.

> (And yes, I know you can do it in C++. I don't like that either, and
> think it was a good idea to get rid of it from Java and C#.)

Which is why I'm glad you can't do this through normal means.

-c


Mike Schilling

11/26/2002 7:36:00 PM

0


"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.184d2e62ce6717129897fb@10.1.1.14...
> cmyers@N0.SP.4M.austin.rr.com wrote:
> > I'd vote for feature.
> >
> > If you wanted to call the GetHashCode on obj's type rather
> > than System.Object, you'd get the MethodInfo from obj's
> > type.
>
> Seems pretty horrible to me - I'd much rather it acted virtually.

Almost all MethodInfos for virtual methods do behave virtually, just as
you'd expect. Object.GetHashCode is, for reasons unknown to me, an
exception. See

http://groups.google.com/groups?&threadm=fNWo9.2399%24Oy1.13702887...
r14.news.prodigy.com

for more (inconclusive) discussion.