473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

Sep 17 '08 #1
3 1479
On Wed, 17 Sep 2008 11:55:05 -0700, Luigi <ci****************@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.SequenceEqual() extension method:

bool result = TestA.SequenceEqual(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
Sep 17 '08 #2
"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.SequenceEqual() extension method:

bool result = TestA.SequenceEqual(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<string,decimal?>?

L
Sep 18 '08 #3
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.SequenceEqual() extension method:

bool result = TestA.SequenceEqual(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<string,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
Sep 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Charles Law | last post by:
Hi folks Not really a .NET question, but I always think this is a good place to ask. Does anyone have a favourite algorithm for determining the similarity of, or difference between two...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
0
by: himoundary | last post by:
hi all i am using microsoft speech sdk (sapi 5.1). i have the following requirements: 1) i need to compare current input sound (through mic) with recorded sound file (say .wav). based of...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.