.NET Hashtable - same key, different hashes

Is it possible for two .net lines to have different hashes? I have a hashtable, among other things a key "path". When I iterate over the elements in a table to print it, I see that the key exists.

When you try to search, however, there is no matching item. Debugging assumes that the string I'm looking for has a different hash than the one I supply as the key.

This code is in the Monorail Castle project, using Brail as a viewer. The key I'm looking for is inserted into the braille line as follows:

UrlHelper.Link(node.CurrentPage.LinkText, {@params: {@path: "/Page1"}})

Then in this method (in a custom IRoutingRule):

public string CreateUrl(System.Collections.IDictionary parameters)
{
    PrintDictionaryToLog(parameters);
    string url;
    if (parameters.Contains("path")) {
        url = (string)parameters["path"];
    }
    else {
        return null;
    }
}

The key is printed in the log, but the function returns null. I did not know that this could even be a problem with .net strings, but I think this is some kind of encoding problem?

, .

, :

2010-03-08 22:58:00,504 [7] DEBUG Knickle.Framework.Routing.PageRoute (null) - Parameters: {System.String controller=null, System.String path=Page1, System.String path=/Page1, System.String action=null, System.String area=null}

, :

parameters.Add("path", "Page1");

, , . .

+3
6

MSDN GetHashCode . , - , , ( ).

http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx

:

, GetHashCode . - . -.

+2

. , URL ..

+1

, , StringComparison.Ordinal, String.Equals, - .

+1

, [Castle.MonoRail.Framework.Services.DefaultUrlBuilder: 397]...

// Forces copying entries to a non readonly dictionary, preserving the original one
parameters = new Hashtable(parameters, StringComparer.InvariantCultureIgnoreCase);

IEqualityComparer , .

( , ), mono 2.10.8.1 (Debian 2.10.8.1-5ubuntu1).

.

+1

Mono ? Mono, , .

Seva , , , - , - .

0

Hashtable . . - . .NET Framework. , .

Hashtable . , Hashtable . , [].

: Hashtable.

Hashtable [#]

using System;
using System.Collections;

class Program
{
    static void Main()
    {
    Hashtable hashtable = new Hashtable();
    hashtable[1] = "One";
    hashtable[2] = "Two";
    hashtable[13] = "Thirteen";

    foreach (DictionaryEntry entry in hashtable)
    {
        Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
    }
    }
}

13, Thirteen
2, Two
1, One

DictionaryEntry, foreach. WriteLine , / .

Foreach

Hashtables, DictionaryEntry foreach-loop. ArrayList. A DictionaryEntry : .

Foreach DictionaryEntry Hashtable. ContainsKey Hashtable . true, .

. [].

, Contains [#]

using System;
using System.Collections;

class Program
{
    static Hashtable GetHashtable()
    {
    // Create and return new Hashtable.
    Hashtable hashtable = new Hashtable();
    hashtable.Add("Area", 1000);
    hashtable.Add("Perimeter", 55);
    hashtable.Add("Mortgage", 540);
    return hashtable;
    }

    static void Main()
    {
    Hashtable hashtable = GetHashtable();

    // See if the Hashtable contains this key.
    Console.WriteLine(hashtable.ContainsKey("Perimeter"));

    // Test the Contains method. It works the same way.
    Console.WriteLine(hashtable.Contains("Area"));

    // Get value of Area with indexer.
    int value = (int)hashtable["Area"];

    // Write the value of Area.
    Console.WriteLine(value);
    }
}

True
True
1000

- , . Hashtable . , . . .

Hashtable. int. / . Hashtable.

, [#]

using System;
using System.Collections;

class Program
{
    static Hashtable GetHashtable()
    {
    Hashtable hashtable = new Hashtable();

    hashtable.Add(300, "Carrot");
    hashtable.Add("Area", 1000);
    return hashtable;
    }

    static void Main()
    {
    Hashtable hashtable = GetHashtable();

    string value1 = (string)hashtable[300];
    Console.WriteLine(value1);

    int value2 = (int)hashtable["Area"];
    Console.WriteLine(value2);
    }
}

Carrot
1000

. - . , InvalidCastException. , is as.

Hashtable. as-operator, . , .

Null : is-operator. true false .

, Hashtable [#]

using System;
using System.Collections;
using System.IO;

class Program
{
    static void Main()
    {
    Hashtable hashtable = new Hashtable();
    hashtable.Add(400, "Blazer");

    // This cast will succeed.
    string value = hashtable[400] as string;
    if (value != null)
    {
        Console.WriteLine(value);
    }

    // This cast won't succeed, but won't throw.
    StreamReader reader = hashtable[400] as StreamReader;
    if (reader != null)
    {
        Console.WriteLine("Unexpected");
    }

    // You can get the object and test it.
    object value2 = hashtable[400];
    if (value2 is string)
    {
        Console.Write("is string: ");
        Console.WriteLine(value2);
    }
    }
}

Blazer : Blazer

Hashtable as-operator. , FxCop, Microsoft.

FxCop * , * . ArrayList. , , ArrayList.

: # Hashtable Keys. .

, ArrayList [#]

using System;
using System.Collections;

class Program
{
    static void Main()
    {
    Hashtable hashtable = new Hashtable();
    hashtable.Add(400, "Blaze");
    hashtable.Add(500, "Fiery");
    hashtable.Add(600, "Fire");
    hashtable.Add(800, "Immolate");

    // Display the keys.
    foreach (int key in hashtable.Keys)
    {
        Console.WriteLine(key);
    }

    // Display the values.
    foreach (string value in hashtable.Values)
    {
        Console.WriteLine(value);
    }

    // Put keys in an ArrayList.
    ArrayList arrayList = new ArrayList(hashtable.Keys);
    foreach (int key in arrayList)
    {
        Console.WriteLine(key);
    }
    }
}

800       (First loop)
600
500
400
Immolate  (Second loop)
Fire
Fiery
Blaze
800       (Third loop)
600
500
400

. , Keys Hashtable. foreach .

. , Hashtable. , Hashtable, .

Console.WriteLine ArrayList. ArrayList Keys ( Values). ArrayList ArrayList.

ArrayList

: . accessers Hashtable .

: , Hashtable.

Count, Clear

Hashtable Count. Clear Hashtable. Hashtable Hashtable().

: , Count. .

, Count Hashtable [#]

using System;
using System.Collections;

class Program
{
    static void Main()
    {
    // Add four elements to Hashtable.
    Hashtable hashtable = new Hashtable();
    hashtable.Add(1, "Sandy");
    hashtable.Add(2, "Bruce");
    hashtable.Add(3, "Fourth");
    hashtable.Add(10, "July");

    // Get Count of Hashtable.
    int count = hashtable.Count;
    Console.WriteLine(count);

    // Clear the Hashtable.
    hashtable.Clear();

    // Get Count of Hashtable again.
    Console.WriteLine(hashtable.Count);
    }
}

4
0

-, Hashtable. , 4. Clear Hashtable, 0 . Hashtable , null.

Count Hashtable. Hashtable , Hashtable. Count .

: MSDN , Count " O (1)".

. . , Hashtable ( ). .

Benchmark

, Hashtable System.Collections Dictionary System.Collections.Generic. .

: , . 20 .

Hashtable, [#]

Hashtable hashtable = new Hashtable();
for (int i = 0; i < 10000; i++)
{
    hashtable[i.ToString("00000")] = i;
}

Dictionary used in benchmark [C#]

var dictionary = new Dictionary<string, int>();
for (int i = 0; i < 10000; i++)
{
    dictionary.Add(i.ToString("00000"), i);
}

Statements benchmarked [C#]

hashtable.ContainsKey("09999")
hashtable.ContainsKey("30000")

dictionary.ContainsKey("09999")
dictionary.ContainsKey("30000")

20

Hashtable result:  966 ms
Dictionary result: 673 ms

Hashtable , . , Hashtable 30% . , .

Hashtable 15 . . Hashtable. , -.

Designer Tips Summary

We used the Hashtable collection. This is an older collection, obsolete from the Dictionary collection. Knowing how to use it is crucial when maintaining old programs. These programs are important for many organizations.

-1
source

Source: https://habr.com/ru/post/1736014/


All Articles