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

  • Home
  • SEARCH
  • 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 903977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:58:34+00:00 2026-05-15T15:58:34+00:00

I’m writing a bijective dictionary class , but I want to ensure the two

  • 0

I’m writing a bijective dictionary class, but I want to ensure the two generic types are not the same type for two reasons.

Firstly, I would like it to implement the IDictionary interface in both directions, but

public class BijectiveDictionary<TKey, TValue>
    : IDictionary<TKey, TValue>, IDictionary<TValue, TKey>

gives me ” ‘BijectiveDictionary<TKey,TValue>’ cannot implement both ‘IDictionary<TKey,TValue>’ and ‘IDictionary<TValue,TKey>’ because they may unify for some type parameter substitutions ” (which is understandable, but undesirable.)

Secondly, I would like to write an optimized solution if both types are the same.

public class BijectiveDictionary<TKey, TValue>
    : IDictionary<TKey, TValue> where TValue : TKey
{
    // Optimized solution
}

public class BijectiveDictionary<TKey, TValue>
    : IDictionary<TKey, TValue>, IDictionary<TValue, TKey> where TValue : !TKey
{
    // Standard solution
}

Is this possible?

If not, I can consider not implementing IDictionary, but I couldn’t guarantee TValue this[TKey key] and TKey this[TValue key] would be different, which would be unfortunate.


It looks like the problem here is that when the two types are the same, the special cases arise.

My original intent was to create a dictionary which maps exactly one key to exactly one value, and visa versa, such that for every KeyValuePair<TKey, TValue>(X, Y), a KeyValuePair<TValue, TKey>(Y, X) exists as well.

When TKey = TValue, then this can be simplified down to a single dictionary:

public T this[T key]
{
    get { return this[key]; }
    set
    {
        base.Add(key, value);
        base.Add(value, key);
    }
}

In this case, you cannot Add(2,3); Add(3,4) because Add(2,3) maps 3 to 2 as well, and [3] would return 2.

However, Jaroslav Jandek’s solution proposed using a second dictionary to do this for cases when TKey != TValue. And although this works wonderfully for those cases, (and what I decided to implement, in the end) it doesn’t quite follow my original intent when TKey = TValue, by allowing Add(2,3); Add(3,4) to map a single key 3 to two values (2 in one direction, and 4 in the other,) though I believe strictly speaking is still a valid bijective function.

  • 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. Editorial Team
    Editorial Team
    2026-05-15T15:58:35+00:00Added an answer on May 15, 2026 at 3:58 pm

    How about this (different approach):

    public class BijectiveDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public BijectiveDictionary<TValue, TKey> Reversed { get; protected set; }
    
        public BijectiveDictionary()
        {
            this.Reversed = new BijectiveDictionary<TValue,TKey>(true);
            this.Reversed.Reversed = this;
        }
    
        protected BijectiveDictionary(bool reversedWillBeSetFromTheCallingBiji) { }
    
        protected void AddRaw(TKey key, TValue value)
        {
            base.Add(key, value);
        }
    
        // Just for demonstration - you should implement the IDictionary interface instead.
        public new void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            this.Reversed.AddRaw(value, key);
        }
    
        public static explicit operator BijectiveDictionary<TValue, TKey>(BijectiveDictionary<TKey, TValue> biji)
        {
            return biji.Reversed;
        }
    }
    

    and in code:

    BijectiveDictionary<int, bool> a = new BijectiveDictionary<int, bool>();
    
    a.Add(5, true);
    a.Add(6, false);
    
    Console.WriteLine(a[5]);// => True
    Console.WriteLine(((BijectiveDictionary < bool, int>)a)[true]);// => 5
    //or
    Console.WriteLine(a.Reversed[true]);// => 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I want to show the soap response to UIWebview.. my soap response is, <p><img

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.