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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:26:06+00:00 2026-05-24T06:26:06+00:00

See comment in Main(). Why can’t I perform the following? public class SomeList<T> :

  • 0

See comment in Main(). Why can’t I perform the following?

   public class SomeList<T> : List<T>
{
    public SomeList(List<T> existing)
    {
        foreach (var item in existing)
            Add(item);
    }

    public override string ToString()
    {
        return "I'm a better list.";
    }
}

internal interface IReadStuff<T>
{
    List<T> ReadStuff();
}

public class ReaderStrategy<Foo> : IReadStuff<Foo>
{
    public List<Foo> ReadStuff()
    {
        return new List<Foo>();
    }
}

public class Foo {}

public class Main
{
    public Main()
    {
        var reader = new ReaderStrategy<Foo>();

        // This works, but type is List<Foo>, not SomeList<Foo>
        List<Foo> aList = reader.ReadStuff();
        // This does not compile, but is what I want to do:
        SomeList<Foo> aBetterList = reader.ReadStuff();
        // This compiles, but always generates null for aBetterList:
        SomeList<Foo> anotherBetterList = reader.ReadStuff() as SomeList<Foo>;
        // This is funky but works:
        SomeList<Foo> works = new SomeList<Foo>(reader.ReadStuff());
    }
}

I am struggling understanding how to use generics with inherited types. I have a need for the above because I want to extend the functionality of List<T> is some special way, for example see SomeList<T> overrides ToString(). However, I want to keep my factory strategy using .Net generic List<T>. Is there a way to make this work?

Edit

I added a constructor that accepts List<T> and adds to SomeList<T>. This doesn’t seem natural, but works. This is an expensive operation, especially if List<T> is large.

My question title was not the best, what I was striving for was an example showing a better way to do this.

  • 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-24T06:26:07+00:00Added an answer on May 24, 2026 at 6:26 am

    reader.ReadStuff() returns List<Foo> – but you are trying to assign it to an object of type SomeList<Foo> which inherits from List<Foo>. This doesn’t work because List<Foo> is not a SomeList<Foo> – it’s the other way round.

    Think about it – it is legal to return a List<Foo> object from ReadStuff() – then you are trying to access functionality on this object that is only available on SomeList<Foo> – this will break and that’s why OOP doesn’t allow you to do this – instances of a child class can be used where an instance of a parent class is expected – but you cannot use a parent class where a child class is expected.

    Going back to your question title: SomeList<T> : List<T> can’t be cast as List<T>? Yes that’s possible, but you are trying to cast List<T> to SomeList<T>.

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

Sidebar

Related Questions

On the developer page (http://developers.facebook.com/docs/reference/plugins/comments/) I can see that the facebook comment Social plugin
In the eclipse variable view, I always see something like comment test string (id=2559)
If you look at the comment here you'll see Lucene is very much the
I was reading a forum recently, and saw this comment: So, you see you've
I don't see synchronized output when i comment the the line wait(1) in thread()
I have a dbf file, and I can see in the view that types
Here I can do conversion from emp class object to emp class object. But
I've got a main WPF window: <Window x:Class=NorthwindInterface.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:ViewModels=clr-namespace:NorthwindInterface.ViewModels Title=MainWindow Height=350 Width=525>
I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons.
I have a main page that loads a content of a div: var content

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.