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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:41:50+00:00 2026-05-12T10:41:50+00:00

Say that you’re writing a library to display things on the screen, so you

  • 0

Say that you’re writing a library to display things on the screen, so you create an IDisplayable interface. This interface has one method to create a control from the object: displayable.GetControl().

You want to create your own list type that can be displayed: MyList<T>. Now this list can only be displayed if T is an IDisplayable, so you could ask in the MyList class that T should implement IDisplayable. But you also want to use this list type in some places when T is not IDisplayable (and as a result this list will not be displayable). So is it possible to say that MyList implements IDisplayable if T implements IDisplayable? I would also be happy if MyList<T> always implements IDisplayable but throws an exception at runtime if you try to call GetControl() if T is not IDisplayable, but I’d like to know if there’s a statically type-safe way to do it. Can this be done? Or am I looking at the wrong solution?

Edit:

I agree with the suggestions so far that MyList may have too many responsibilities. My original idea was to create a MyDisplayableList<T> : MyList<T> (where T : IDisplayable).

The problem with this approach is that I have a lot of methods that take a MyList and return a MyList (for example methods like Select in Linq). So if I use select on an MyDisplayableList I get back a MyList and them I’m unable to display it even though it is a MyList…is there a type safe way to handle this problem in C#?

  • 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-12T10:41:50+00:00Added an answer on May 12, 2026 at 10:41 am

    Simple. Check if the type is IDisplayable. If it’s not, throw an InvalidOperationException:

    if (!typeof(IDisplayable).IsAssignableFrom(typeof(T))) 
        throw new InvalidOperationException();
    

    Or if you have an instance of T, simply check with that:

    IDisplayable disp = instanceOfT as IDisplayable;
    if (disp == null)
        throw new InvalidOperationException();
    // do stuff with `disp`.
    

    Your design might be flawed though. You might be putting too much in a class and violating single responsibility principle. Recheck your design first.

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

Sidebar

Related Questions

Say that I have made some software (which has both library form and can
Say that I have two html nodes like this: <div class='node a'>one</div> <input class='node
Say that I have a base class with a class method: @interface base :
Lets say that one has a class like Person that has associated with it
Say that I have a C program and it has this line: int a
Say that Blog model has a one-to-many relationship with Entry model. In a form,
Say that I have a data model called Widget . If I pick one
Say that a method only reads data from a database and does not write
Say that I have an article with multiple pages. Each page has a short
Say that i have a generic dictionary with data like this (I hope the

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.