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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:03:15+00:00 2026-05-18T11:03:15+00:00

In the below hierarchy, is there a way at compile-time to exclude IBars while

  • 0

In the below hierarchy, is there a way at compile-time to exclude IBars while still allowing IFoos?

Example:

//IFoo defines functionality that Snafu needs to use, and so is a type restriction.
public interface IFoo {...}

//IBar defines supplemental required functionality that Snafu does not support.
public interface IBar:IFoo {...}

//Can I generate a compiler error if an IBar is used as the generic type?
public class Snafu<T> where T:IFoo
{
    public void DoSomethingWith(T myFoo)
    {
        //Best thing I can think of with the current hierarchy
        if(myFoo is IBar) throw new ArgumentException("IBars are not supported");
    }
}

The only thing I can think of that works is to define a third “flag” interface, and apply it to all IFoos that are not IBars. With IFoo and IBar defined:

public interface IAmNotAnIBar:IFoo {}

public class Snafu<T> where T:IAmNotAnIBar {...}

However, this smells like a hack; the new interface doesn’t define anything new that IFoo doesn’t, and developers have to know not to use IFoo (IIRC you can’t hide IFoo by making it internal, while still exposing the other interfaces as public), so they can bypass the compile-time check in good or bad faith by just implementing IFoo. Is there anything more elegant?

EDIT: Thanks to all for the responses so far. Maybe a more concrete example would illustrate why I asked the question in the first place.

Let’s say that IFoo defines basic functionality of a domain object that is mapped to the DB. It must have a read-write ID property, maybe the ability to emit and absorb a DTO, whatever. There are several areas of the system that deal with these object as IFoos, including persistence: IFoos are read and written to persistent storage using a Repository that can handle any IFoo (the class Snafu in the above example; DoSomething() performs some persistent action on a strongly-typed IFoo).

Now, let’s define IBar as a domain object that is populated differently from IFoos. They define, in addition to IFoo functionality, some additional Initialize() method that ensures they are in a consistent state. They are still domain objects (IFoos) and should still be treated as such in all areas of the system, EXCEPT that you cannot pass an IBar to an instance of Snafu and expect correct results, because Snafu does not and cannot call the method properly (let’s say Initialize() takes a parameter that is an external dependency which Snafu doesn’t have and should not be given). Instead, you should call a different class that is the Repository for all IBars. My goal was, at compile-time, to alert the developer that they were doing something wrong, instead of relying on adequate run-time testing (unit, integration, functional, manual, etc) of every possible call to DoSomething() on Snafu to ensure it’s never passed an IBar.

  • 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-18T11:03:16+00:00Added an answer on May 18, 2026 at 11:03 am

    If you’re trying to Exclude IBars while allowing IFoos, that tells me that IBar should not really inherit from IFoo (they’re not truly a parent -> child relationship).

    Remember that just because two interfaces share members of the same name it doesn’t mean that one should inherit from the other. If you use inheritance like that, you’re giving a new meaning to the members inherited from the parent thus violating the Liskov Substitution Principle. In your case, it’s obvious that IBar IS NOT also an IFoo…otherwise you wouldn’t need to restrict their use.

    My guess is there’s a way to refactor your interfaces so that things make a little more sense but without more details, it’s hard for me to say.

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

Sidebar

Related Questions

I have a hierarchy in my website project as below: [CustomControl1 - folder] -
I get compile errors with this overcomplicated class hierarchy. I wonder if it has
I have a small class hierarchy that I'm having trouble implementing copyWithZone: for. I've
I have a class hierarchy representing different language constructs: Expression <- NumericLiteral UnaryExpression BinaryExpression
I have the below html that is supposed to create a fly out Menu.
I have a RESTful service with the interface below, using POST. When I try
I the context of a Java application I am using SQLIte to persist some
This is not a homework. Visually it looks like a tree, but all leafs
How would I go about resizing a div from a page to it's master

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.