Similarity between Dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Similarity between Dictionaries

    Hi all,
    I have two Dictionary exactly the same, but if I try to verify this
    likeness, I get false, like this code:

    Dictionary<int, stringTestA = new Dictionary<int, string>();
    Dictionary<int, stringTestB = new Dictionary<int, string>();

    TestA.Add(1, "TestString ");
    TestB.Add(1, "TestString ");

    bool result = (TestA == TestB); // returns "false"

    How can I check the similarity of 2 dictionaries?
    (I'm using C# 2.0)

    Thanks in advance.

    Luigi



  • Peter Duniho

    #2
    Re: Similarity between Dictionaries

    On Wed, 17 Sep 2008 11:55:05 -0700, Luigi <ciupazNoSpamGr azie@inwind.it>
    wrote:
    Hi all,
    I have two Dictionary exactly the same, but if I try to verify this
    likeness, I get false, like this code:
    >
    Dictionary<int, stringTestA = new Dictionary<int, string>();
    Dictionary<int, stringTestB = new Dictionary<int, string>();
    >
    TestA.Add(1, "TestString ");
    TestB.Add(1, "TestString ");
    >
    bool result = (TestA == TestB); // returns "false"
    Just for the record, that fails because the Dictionary class doesn't
    provide an operator == overload. It also doesn't override
    Object.Equals() , so you couldn't use that as a workaround either.

    If you had been using C# 3.0 with .NET 3.5, you would have been able to
    use the Enumerable.Sequ enceEqual() extension method:

    bool result = TestA.SequenceE qual(TestB);

    But if you're stuck with C# 2.0, I think you'll have to essentially
    implement the iterative comparison yourself. I'm not aware of a built-in
    implementation of something like this for the Dictionary class.

    Pete

    Comment

    • =?Utf-8?B?THVpZ2k=?=

      #3
      Re: Similarity between Dictionaries

      "Peter Duniho" wrote:
      Just for the record, that fails because the Dictionary class doesn't
      provide an operator == overload. It also doesn't override
      Object.Equals() , so you couldn't use that as a workaround either.
      >
      If you had been using C# 3.0 with .NET 3.5, you would have been able to
      use the Enumerable.Sequ enceEqual() extension method:
      >
      bool result = TestA.SequenceE qual(TestB);
      >
      But if you're stuck with C# 2.0, I think you'll have to essentially
      implement the iterative comparison yourself. I'm not aware of a built-in
      implementation of something like this for the Dictionary class.
      Thanks Pete,
      and how can I solve this, with a dictionary<stri ng,decimal?>?

      L

      Comment

      • Hans Kesting

        #4
        Re: Similarity between Dictionaries

        Luigi expressed precisely :
        "Peter Duniho" wrote:
        >Just for the record, that fails because the Dictionary class doesn't
        >provide an operator == overload. It also doesn't override
        >Object.Equals( ), so you couldn't use that as a workaround either.
        >>
        >If you had been using C# 3.0 with .NET 3.5, you would have been able to
        >use the Enumerable.Sequ enceEqual() extension method:
        >>
        > bool result = TestA.SequenceE qual(TestB);
        >>
        >But if you're stuck with C# 2.0, I think you'll have to essentially
        >implement the iterative comparison yourself. I'm not aware of a built-in
        >implementati on of something like this for the Dictionary class.
        >
        Thanks Pete,
        and how can I solve this, with a dictionary<stri ng,decimal?>?
        >
        L
        Something like this:
        If the sizes of the dictionaries are not equal, then they don't contain
        the same items.
        If the sizes are equal, loop through all items of dictionaryA, and test
        if the key is present in dictionaryB with the same value. When a
        mismatch is found, the dictionaries are not equal. When you have tested
        all items and didn't find a mismatch, then they are equal.

        Hans Kesting


        Comment

        Working...