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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:23:08+00:00 2026-05-23T18:23:08+00:00

I have a interface that inherits from IList: public interface IBase {} public class

  • 0

I have a interface that inherits from IList:

public interface IBase {}
public class Derived : IBase {}
public interface IMyList : IList<IBase> {}

I want to cast a variable of type IMyList to type IList<Derived> or List<Derived>, whichever is easier or makes the most sense. What’s the best way to do this?

Note that I’m using .NET 3.5

  • 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-23T18:23:09+00:00Added an answer on May 23, 2026 at 6:23 pm

    A direct cast isn’t going to be suitable since there are numerous problems that could occur (IMyList may contain types other than Derived, etc.)

    As long as you’re not modifying the list (in which case, IEnumerable<Derived> would work) you could simply:

    var validItems = myList.Cast<Derived>();
    

    And then iterate over the result.

    Edit

    Based on the OP’s comment below, there are two other things to mention:

    1) If you need an IList<Derived>, you could simply add to the above and call myList.Cast<Derived>().ToList();

    2) If those are truly the requirements, then your code doesn’t make any sense. If IMyList should only ever contain Derived objects then IMyList should derive from IList<Derived>. Remember that while you know that there is only one type implementing the IBase interface, the compiler doesn’t so be specific.

    Using interfaces everywhere just for the sake of using interfaces doesn’t help anybody!

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

Sidebar

Related Questions

I have an interface for a base class, and every class that inherits from
Please help Stackoverflow! I have a MapInterface class in AS3 that inherits from Interface
I have an IRepository interface that inherits from IRepository<TObject> . I also have a
If we have a class that inherits from multiple interfaces, and the interfaces have
I have an interface, and some classes that inherit from it. public interface IFoo
I have a class that inherits a generic dictionary and an inteface public class
I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles
I have GraphicView class that inherits from UIView. Its initWithFrame method is: @implementation GraphicsView
I have a class e.g. DerivedClass that inherits from a base class e.g. BaseClass.
I have a class that inherits from CCSprite called GameObject . I also have

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.