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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:01:45+00:00 2026-05-27T00:01:45+00:00

If one is designing an interface like ILookup (for simplicity, assume it’s simply ICheckIfContained

  • 0

If one is designing an interface like ILookup (for simplicity, assume it’s simply ICheckIfContained, so the type of the item’s value wouldn’t matter), what would be the ideal type of variance with respect to the first parameter?

Checking to see if an ICheckIfContained<Animal> contains a particular Cat would be a natural operation, and would be type-safe under normal rules of contravariance.

Checking to see if an ICheckIfContained<Cat> contains a particular Animal would not be type-safe under normal rules of contravariance, but would be natural and well-defined (if the Animal in question is not a Cat, the proper behavior would be for the ICheckIfContained to say it does not contain the animal in question).

Checking to see if an ICheckIfContained<Cat> contains a particular Dog would not be particularly useful (the answer would always be “no”) but the answer would be well-defined. Note that the answer in this scenario, unlike the previous one, could be determined statically.

One approach would be to have an ICheckIfContained be a non-generic interface, and simply accept a parameter of type Object. This would seem rather inefficient, however, in cases where the list contained nothing but a single type of struct. On the other hand, if it’s a generic interface, then the compiler would be unable to allow its use in the covariant scenario (passing in a parameter whose declared type is a parent of the expected type, such that the passed-in object might or might not be of the expected type).

Is there any nice pattern for getting the efficiency of generics in cases where the compiler can confirm that everything is the same type, while still allowing the general-case behavior of checking for whether an object of parent type is in the collection of child type?

  • 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-27T00:01:46+00:00Added an answer on May 27, 2026 at 12:01 am

    I do not think you can do that. It would definitely be efficient to be able to statically check this to false, but passing Dog to Contains(cat) can be optimized only if compiler knows exactly what kind of operation is performed by this method.

    What if your Contains is actually Add? Compiler does not understand method names.

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

Sidebar

Related Questions

I'm designing an API interface for a magazine viewer. Something like iGizmo (for iPad).
At work I see one colleague designing a site in Photoshop/Fireworks, I see another
When designing tables, I've developed a habit of having one column that is unique
I am designing a cookbook website and I am stuck on this one problem.
I'm designing a networking framework which uses WSAEventSelect for asynchronous operations. I spawn one
I am currently designing a forum as a personal project. One of the recurring
On a page that I'm designing I have a form with one input of
I'm in the early phases of designing an Azure-based application. One of the things
How would one go about defining something like this in an xsd? <start> <request
I'm designing a class that has two dependencies. One of the dependency classes has

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.