Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
GeneralRe: Question about Weak Events... Pin
SledgeHammer0119-Apr-11 10:06
SledgeHammer0119-Apr-11 10:06 
GeneralRe: Question about Weak Events... Pin
DaveyM6919-Apr-11 12:13
professionalDaveyM6919-Apr-11 12:13 
GeneralRe: Question about Weak Events... Pin
SledgeHammer0119-Apr-11 12:27
SledgeHammer0119-Apr-11 12:27 
GeneralRe: Question about Weak Events... Pin
Mark198619-Apr-11 19:36
Mark198619-Apr-11 19:36 
QuestionHow To Use a C Libray in your C# Program Pin
Andy20219-Apr-11 0:43
Andy20219-Apr-11 0:43 
AnswerRe: How To Use a C Libray in your C# Program Pin
Luc Pattyn19-Apr-11 1:05
sitebuilderLuc Pattyn19-Apr-11 1:05 
AnswerRe: How To Use a C Libray in your C# Program Pin
Rick Shaub19-Apr-11 10:18
Rick Shaub19-Apr-11 10:18 
QuestionHashtable/IDictionary - getHashCode and Equals [modified] Pin
devvvy18-Apr-11 23:34
devvvy18-Apr-11 23:34 
Hi
I always thought IDictionary checks key when adding elements by calling GetHashCode overrides - but I did a simple experiment, seems like it only call Equals (i.e. Even if two instances with same hash code added to a Hashtable or Dictionary, it won't blow up so long "Equals" return false)

<br />
public class Complex<br />
    {<br />
        public int a, b, c;<br />
<br />
        public override bool Equals(object obj)<br />
        {<br />
            if (obj is Complex)<br />
            {<br />
                Complex other = (Complex)obj;<br />
                if (other.a == this.a <br />
                    && other.b == this.b <br />
                    && other.c == this.c)<br />
                {<br />
                    return true;<br />
                }<br />
            } <br />
<br />
            return false;<br />
        }<br />
<br />
        public override int GetHashCode()<br />
        {<br />
            return a.GetHashCode() ^ b.GetHashCode(); // Purpose to omit "c" is to test behavior if Equals and GetHashCode compute differently<br />
        }<br />
    }<br />
...<br />
Complex c1 = new Complex();<br />
                c1.a = 1;<br />
                c1.b = 2;<br />
                c1.c = 3;<br />
                Complex c2 = new Complex();<br />
                c2.a = 1;<br />
                c2.b = 2;<br />
                c2.c = 5;<br />
<br />
                System.Collections.Hashtable SomeDict = new System.Collections.Hashtable();<br />
                SomeDict.Add(c1, DateTime.Now);<br />
                SomeDict.Add(c2, DateTime.Now);<br />


I digged around MSDN[^] and found that
"Override the Equals method whenever you implement the equality operator (==), and make them do the same thing. This allows infrastructure code such as Hashtable and ArrayList, which use the Equals method, to behave the same way as user code written using the equality operator."

So this confirms Hashtable or Dictionary uses only "Equals", not "GetHashCode" --- then what's "GetHashCode" for? Doesn't seems like "Hashtable" or "Dictionary<k,v>" are using it at all..?!?

Another REF (but this talks about performance only): http://www.codethinked.com/an-overview-of-system_collections_generic[^]
dev
modified on Tuesday, April 19, 2011 5:41 AM

AnswerRe: Hashtable/IDictionary - getHashCode and Equals Pin
David198718-Apr-11 23:56
David198718-Apr-11 23:56 
GeneralRe: Hashtable/IDictionary - getHashCode and Equals Pin
Md. Marufuzzaman19-Apr-11 2:06
professionalMd. Marufuzzaman19-Apr-11 2:06 
AnswerRe: Hashtable/IDictionary - getHashCode and Equals Pin
Luc Pattyn19-Apr-11 0:02
sitebuilderLuc Pattyn19-Apr-11 0:02 
AnswerCORRECTION - Dictionary did throw an ArgumentException if insert two items where Equals returned true Pin
devvvy24-Apr-11 23:43
devvvy24-Apr-11 23:43 
QuestionWhere to Place FullAddress Pin
Anubhava Dimri18-Apr-11 21:24
Anubhava Dimri18-Apr-11 21:24 
AnswerRe: Where to Place FullAddress Pin
Wayne Gaylard18-Apr-11 21:32
professionalWayne Gaylard18-Apr-11 21:32 
AnswerRe: Where to Place FullAddress Pin
Keith Barrow18-Apr-11 22:39
professionalKeith Barrow18-Apr-11 22:39 
GeneralRe: Where to Place FullAddress Pin
Anubhava Dimri18-Apr-11 22:46
Anubhava Dimri18-Apr-11 22:46 
GeneralRe: Where to Place FullAddress Pin
Keith Barrow18-Apr-11 22:57
professionalKeith Barrow18-Apr-11 22:57 
GeneralRe: Where to Place FullAddress Pin
Anubhava Dimri18-Apr-11 23:22
Anubhava Dimri18-Apr-11 23:22 
GeneralRe: Where to Place FullAddress Pin
Dave Kreskowiak19-Apr-11 1:49
mveDave Kreskowiak19-Apr-11 1:49 
GeneralRe: Where to Place FullAddress Pin
Pete O'Hanlon19-Apr-11 2:30
mvePete O'Hanlon19-Apr-11 2:30 
AnswerRe: Where to Place FullAddress Pin
jschell19-Apr-11 8:56
jschell19-Apr-11 8:56 
AnswerRe: Where to Place FullAddress Pin
Prasanta_Prince19-Apr-11 22:52
Prasanta_Prince19-Apr-11 22:52 
Questionwindows service with digital certificate token Pin
piticcotoc18-Apr-11 3:52
piticcotoc18-Apr-11 3:52 
AnswerRe: windows service with digital certificate token Pin
#realJSOP18-Apr-11 5:32
mve#realJSOP18-Apr-11 5:32 
GeneralRe: windows service with digital certificate token Pin
piticcotoc18-Apr-11 23:00
piticcotoc18-Apr-11 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.