Click here to Skip to main content
15,886,258 members
Home / Discussions / C#
   

C#

 
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 
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 
CORRECTION - Dictionary did throw an ArgumentException if insert two items (where "Equals" returned true) gets inserted into dictionary twice (just set "j" to a bigger number, say 2)

However, below I've tried to implement GetHashCode in two ways:
ATTEMP 1 (correct because "GetHashCode" aligned with "Equals") --- 46 seconds to recall the elements by Key
ATTEMP 2 (Incorrect because not aligned with implementation in "Equal") --- 49 seconds (Seems like negligible and diff really arise from the addition XOR operation in GetHashCode)

You can try to implement GetHashCode = a^b, it'd be done in no time.

I'm not really sure why we need "GetHashCode" still - (unconvinced that its uses it's primarily internal to Dictionary's internal retrieval and impact only speed)

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;<br />
<br />
namespace TestDictWithDuplKeys<br />
{<br />
    public class SomeClass<br />
    {<br />
        public int a = 0;<br />
        public int b = 0;<br />
        public int c = 0;<br />
        public int d = 0; // Not part of Primary key so excluded from "Equal"<br />
<br />
        public override bool Equals(object obj)<br />
        {<br />
            SomeClass cObj = null;<br />
<br />
            if (obj != null && obj is SomeClass)<br />
            {<br />
                cObj = (SomeClass)obj;<br />
                if (this.a == cObj.a && this.b == cObj.b && this.c == cObj.c)<br />
                {<br />
                    return true;<br />
                }<br />
                else<br />
                {<br />
                    return false;<br />
                }<br />
            }<br />
            else<br />
            {<br />
                return false;<br />
            }<br />
        }<br />
<br />
        public override int GetHashCode()<br />
        {<br />
            // NOTE: Even though ATTEMP 2 is "Wrong", I see only slight performance degradation (from 45 sec to 49 sec)<br />
            int HashCode = a ^ b ^ c; // ATTEMP 1: Correct! Because it's same as in "Equal"<br />
            // int HashCode = a ^ b ^ c ^ d; // ATTEMPT 2: GetHashCode should return same value for "Equal" instances - this means implementation should include "c" (same as in "Equal"!)<br />
            return HashCode;<br />
        }<br />
    }<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            IDictionary<SomeClass, string> SomeDict = null;<br />
            SomeClass o = null;<br />
            const int MAXITEMS = 1000000;<br />
            try<br />
            {<br />
                #region Add to dictionary, with duplicate keys!<br />
                SomeDict = new Dictionary<SomeClass, string>(MAXITEMS);<br />
                for (int j = 0; j < 1; j++)<br />
                {<br />
                    for (int i = 0; i < MAXITEMS; i++)<br />
                    {<br />
                        o = new SomeClass();<br />
                        o.a = i;<br />
                        o.b = i * 2;<br />
                        o.c = i * 3;<br />
                        o.d = i * 4;<br />
<br />
                        SomeDict.Add(o, Guid.NewGuid().ToString());<br />
                    }<br />
<br />
                    Console.WriteLine("Pass #" + j);<br />
                }<br />
                #endregion<br />
                Console.WriteLine("Finished adding elements to dict....");<br />
<br />
                #region Retrieve from dictionary<br />
                DateTime Start = DateTime.Now;<br />
                foreach(KeyValuePair<SomeClass, string> Entry in SomeDict)<br />
                {<br />
                    string Value = SomeDict[Entry.Key];<br />
                    // Console.WriteLine(Value);<br />
                }<br />
                #endregion<br />
                DateTime End = DateTime.Now;<br />
<br />
                Console.WriteLine("Duration(sec):" + End.Subtract(Start).TotalSeconds);<br />
            }<br />
            catch (Exception Ex)<br />
            {<br />
                Console.WriteLine(Ex.Message);<br />
                Console.ReadLine();<br />
            }<br />
<br />
            return;<br />
        }<br />
    }<br />
}<br />

dev

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 
AnswerRe: windows service with digital certificate token Pin
BobJanova18-Apr-11 23:13
BobJanova18-Apr-11 23:13 
GeneralRe: windows service with digital certificate token Pin
piticcotoc18-Apr-11 23:15
piticcotoc18-Apr-11 23:15 
QuestionRe: windows service with digital certificate token Pin
#realJSOP19-Apr-11 3:36
mve#realJSOP19-Apr-11 3:36 
AnswerRe: windows service with digital certificate token Pin
#realJSOP19-Apr-11 3:40
mve#realJSOP19-Apr-11 3:40 

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.