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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:16:15+00:00 2026-05-15T18:16:15+00:00

(This is .Net 3.5) I have a class FooList which implements IList and a

  • 0

(This is .Net 3.5) I have a class FooList which implements IList and a class FooClass which implements IFoo. A user requires IList<IFoo>. In my implementation, I create a FooList<FooClass>, called X. How do I code my return so that my FooList<FooClass> X becomes his IList<IFoo>?

If I try

return X.Cast( ).ToList( );

he gets an IList<IFoo>, but it is not my FooList; it is a List, and a new one at that.

  • 1 1 Answer
  • 3 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-15T18:16:16+00:00Added an answer on May 15, 2026 at 6:16 pm

    This isn’t going to work out, because a FooList<FooClass> is not an IList<IFoo>. This is why:

    var myList = new FooList<FooClass>();
    IFoo obj = new SomeOtherFooClass();
    IList<IFoo> result = (IList<IFoo>)myList; // hypothetical, wouldn't actually work
    result.Add(obj); // uh-oh, now myList has SomeOtherFooClass
    

    You need to either make a copy or use an interface that is actually covariant on the contained type, like IEnumerable<T> instead of IList<T>. Or, if appropriate, you should declare your FooList<FooClass> as an FooList<IFoo> from the get-go instead.

    Here is a small implementation that demonstrates my second suggestion:

    public interface IFoo { }
    public class FooClass : IFoo { }
    
    public class FooList<T> : IList<T>
    {
        public void RemoveAt(int index) { /* ... */ }
        /* further boring implementation of IList<T> goes here */
    }
    
    public static void ListConsumer(IList<IFoo> foos)
    {
        foos.RemoveAt(0); // or whatever
    }
    
    public static IList<IFoo> ListProducer()
    {
        // FooList<FooClass> foos = new FooList<FooClass>(); // would not work
        FooList<IFoo> foos = new FooList<IFoo>();
    
        foos.Add(new FooClass());
    
        return foos; // a FooList<IFoo> is an IList<IFoo> so this is cool
    }
    
    public static void Demo()
    {
        ListConsumer(ListProducer()); // no problemo
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this middleware which is the first: class RedirectIt require net/https require uri
This is .NET 4.0/C#. I have a class that inherits from an interface: public
I don't have any idea how to use this class in .net. Anyone wants
I have a custom control that inherits from .NET's CompositeControl class. This control overrides
I am trying to understand what ThreadPool does, I have this .NET example: class
In a ASP.Net application I have a class. This class need reference to two
I have this class package net.omnosis.mazegame.components; import net.omnosis.mazegame.SlicedBitmap; import android.graphics.Bitmap; public class PlayerLayer extends
In .NET I have a class called Caption. I have another class called Gauge.
I have this ASP.NET app, in which I use Entity Framework to connect to
Say we have a class like this: import java.net.URL import xml._ class SearchData(xml: Node)

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.