Solved

C# Dictionary<string, string>

Posted on 2011-03-06
5
916 Views
Last Modified: 2013-12-17
I'm testing this sample code I got from www.dotnetperls.com .

Question:
1. What is the default hash algorithm being used for the key strings?

I see:
"string1" hashed to 0x2163a1a6
"string2" hashed to 0x440b1641

Is this a good hash algorithm for long strings?

using System;
using System.Collections.Generic;

namespace dicstring
{
    class Program
    {
        static void Main(string[] args)
        {
            // A. Create a new Dictionary with two keys and two values.
            Dictionary<string, string> example = new Dictionary<string, string>();
            example.Add("string1", "C#");
            example.Add("string2", "Perl");

            // B. Lookup a string in the Dictionary.
            string value;
            if (example.TryGetValue("string2", out value))
            {
                Console.WriteLine("Found {0}", value);
            }

            // C. See if it contains this key.
            Console.WriteLine(example.ContainsKey("string3"));

            // D. Add this key if it isn't there.
            if (!example.ContainsKey("string3"))
            {
                example.Add("string3", "VB.NET");
            }

            // E. Enumerate the keys
            foreach (var pair in example)
            {
                Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value);
            }
        }
    }
}

Open in new window

0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:lalitgada
ID: 35053604
what u want to know?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35053962
>>Is this a good hash algorithm for long strings?

Yes, because it works.  The algorithm MUST result in different values for different entries.  Do not attempt to write your own algorithm unless you know what you are doing.
0
 

Author Comment

by:deleyd
ID: 35057971
How can I find out what the hash algorithm is? Is it documented somewhere? Or do I just have to trace the code internals to find out the hard way?
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35060319
I used .NET Reflector to disassemble String.GetHashCode() (.NET Framework 4).  Here is the result:

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), SecuritySafeCritical]
public override unsafe int GetHashCode()
{
    fixed (char* str = ((char*) this))
    {
        char* chPtr = str;
        int num = 0x15051505;
        int num2 = num;
        int* numPtr = (int*) chPtr;
        for (int i = this.Length; i > 0; i -= 4)
        {
            num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];
            if (i <= 2)
            {
                break;
            }
            num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];
            numPtr += 2;
        }
        return (num + (num2 * 0x5d588b65));
    }
}

Open in new window


Here is a note from the String.GetHashCode() documentation:
http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx

The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen is to improve the performance of GetHashCode.
Note Note

Notes to Callers

The value returned by GetHashCode is platform-dependent. It differs on the 32-bit and 64-bit versions of the .NET Framework.
0
 

Author Closing Comment

by:deleyd
ID: 35064278
Thank you for alerting me to .NET Reflector
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 100
PowerShell:  Where-Object not working as a filter 2 71
How to force output to ascii 2 32
ComboBox to String Not Working 5 13
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question