Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:39:54+00:00 2026-05-11T06:39:54+00:00

What is the best way to make a generic equivalence function for two Dictionaries,

  • 0

What is the best way to make a generic equivalence function for two Dictionaries, whose keys and values are value types?

Right now I have a Dictionary<string, bool>, and have created an extension method that (I think) works to test for equivalence between two Dictionary<string, bool>.

I wanted to make it more generic. And my first thought was to make it like this:

public static bool EquivalentTo<K, V>(this IDictionary<K, V> lhs,                                             IDictionary<K, V> rhs)          where K: struct          where V: struct     { } 

However, this doesn’t work because strings are immutable reference types, and NOT a value type.

So how would one go about generic-izing my original Equivalence test for Dictionary<string, bool> ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:39:54+00:00Added an answer on May 11, 2026 at 6:39 am

    Why do you want to restrict K and V to being value types in the first place? I suggest you just remove the constraints. There are ‘interesting’ things about dictionaries though – are two dictionaries which happen to have the same entries, but use different equality comparers equivalent? IDictionary<,> doesn’t have an equality comparer property, unfortunately, so you may need to provide on to your equivalence method. You’ll need to consider what it even means to be equivalent here.

    For example, two dictionaries both with case-insensitive equality comparers might have { ‘FOO’, true } and { ‘foo’, true } – to some extent they’re equivalent, but to some extent they aren’t. It depends on what you want to use the equivalence relation for.

    EDIT: Here’s an example which should be fine in most cases, but could give odd results if the two dictionaries treat keys differently:

    public static bool EquivalentTo<TKey, TValue>(      this IDictionary<TKey, TValue> first,      IDictionary<TKey, TValue> second) {      return first.EquivalentTo(second, EqualityComparer<TValue>.Default); }  public static bool EquivalentTo<TKey, TValue>(      this IDictionary<TKey, TValue> first,      IDictionary<TKey, TValue> second,      IEqualityComparer<TValue> valueComparer) {     if (first == second)     {         return true;     }     if (first == null || second == null)     {         return false;     }     if (first.Count != second.Count)     {         return false;     }     foreach (var firstKeyValue in first)     {         TValue secondValue;         if (!second.TryGetValue(firstKeyValue.Key, out secondValue) ||             !valueComparer.Equals(firstKeyValue.Value, secondValue))         {             return false;         }     }     return true; } 

    Untested, but let me know if it does what you want…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to make a function that has pointer as argument work
what's the best way to make communications between page widgets residing on differents ViewPart?
What is the best way to make an HTTP GET request in Ruby with
What is the best way to make sure that all the environment variables I
What is the best way to make my Qt Ruby application into an executable
I'm wondering what is the best way to make data thread-safe. Specifically, I need
I am wondering whats the best way to programmatically make rounded corners for images.
How I can make a Makefile, because it's the best way when you distribute
I want to make a code snippet database web application. Would the best way
Hopefully this explanation will make sense, but what is the best way (if it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.