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 6814567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:42:15+00:00 2026-05-26T20:42:15+00:00

Is it possible to implement a class constrained to two unique generic parameters? If

  • 0

Is it possible to implement a class constrained to two unique generic parameters?

If it is not, is that because it is unimplemented or because it would be impossible given the language structure (inheritance)?

I would like something of the form:

class BidirectionalMap<T1,T2> where T1 != T2
{
  ...
}

I am implementing a Bidirectional dictionary. This is mostly a question of curiosity, not of need.


Paraphrased from the comments:

  1. Dan: “What are the negative consequence if this constraint is not met?”

  2. Me: “Then the user could index with map[t1] and map[t2]. If they were the same type, there would be no distinction and it wouldn’t make any sense.”

  3. Dan: The compiler actually allows [two generic type parameters to define distinct method overloads], so I’m curious; does it arbitrarily pick one of the methods to call?

  • 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-26T20:42:15+00:00Added an answer on May 26, 2026 at 8:42 pm

    Expanding on the example to highlight the problem:

    public class BidirectionalMap<T1,T2>
    {
        public void Remove(T1 item) {}
        public void Remove(T2 item) {}
    
        public static void Test()
        {
            //This line compiles
            var possiblyBad = new BidirectionalMap<string, string>();
    
            //This causes the compiler to fail with an ambiguous invocation
            possiblyBad.Remove("Something");
        }
    }
    

    So the answer is that, even though you can’t specify the constraint T1 != T2, it doesn’t matter, because the compiler will fail as soon as you try to do something that would violate the implicit constraint. It still catches the failure at compile time, so you can use these overloads with impunity. It’s a bit odd, as you can create an instance of the map (and could even write IL code that manipulates the map appropriately), but the C# compiler won’t let you wreak havoc by arbitrarily resolving ambiguous overloads.


    One side note is that this kind of overloading could cause some odd behaviors if you’re not careful. If you have a BidirectionalMap<Animal, Cat> and Cat : Animal, consider what will happen with this code:

    Animal animal = new Cat();
    map.Remove(animal);
    

    This will call the overload that takes Animal, so it will try to remove a key, even though you might have intended to remove the value Cat. This is a somewhat artificial case, but it’s enough to warrant caution when very different behaviors occur as a result of method overloading. In such cases, it’s probably easier to read and maintain if you just give the methods different names, reflecting their different behaviors (RemoveKey and RemoveValue, let’s say.)

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

Sidebar

Related Questions

Is it possible to implement a class template in such a way that one
In C++, is it possible to have a base plus derived class implement a
Given a class instance, is it possible to determine if it implements a particular
Possible Duplicate: Define a generic that implements the + operator I am recently working
Is it possible to implement friend function and friend class (as in c++) in
Is it possible to implement static class member functions in *.cpp file instead of
Is it possible to implement an abstract base class with members inherited from another
Is it possible to implement RequestContext methods outside an @Entity-annotated class? @Entity class TheEntity
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
Is it possible to implement autoboxing for your own classes? To illustrate my example,

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.