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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:47:20+00:00 2026-06-04T18:47:20+00:00

I have an abstract parent class which child classes that inherit from it. I

  • 0

I have an abstract parent class which child classes that inherit from it. I have another class that contains many List<> types of the different child classes. I then have a method in another class that takes a parameter of List<ParentType> and just calls the methods that are declared abstract.

I’m having a problem using List<T>.Cast<T2> on the lists of the child classes. I’m getting the error:

System.Linq.Enumerable.Cast(System.Collections.IEnumerable)’ is a ‘method’, which is not valid in the given context

Does anybody know how to fix this error? Or do I have to reconstruct a list of type List<ParentType> and recast each item individually?

What I’m trying to do:
public abstract class P {
public int num;
public abstract double addSections();
}

public class A : P {
    public int num2;
    public A(int r, int n) {
        num = r;
        num2 = n;
    }
    public double addSections() { return (double)num + (double)num2; }
}

public class B : P {
    public double g;
    public B(int r, double k) {
        num = r;
        g = k;
    }
    public double addSections() { return (double)num + g; }
}

public class MyClass {
    public MyClass() {
        List<A> listA;
        List<B> listB;
        //...
        helper(listA.Cast<P>()); //doesn't work
        helper(listB.Cast<P>().ToList()); //doesn't work either
    }

    public void helper(List<P> list) {
        //...
    }
}
  • 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-06-04T18:47:22+00:00Added an answer on June 4, 2026 at 6:47 pm

    In lieu of actually seeing your code so we can fix it, how about changing the method instead:

    public void DoSomething<T>(IEnumerable<T> items) where T : ParentType
    {
        ... 
    }
    

    Or if you’re using C# 4 and .NET 4, this should be fine, as IEnumerable<T> is covariant in T in .NET 4.

    public void DoSomething(IEnumerable<ParentType> items)
    {
        ... 
    }
    

    Do you really need the method to accept a List<ParentType>? After all, if you’re going to call:

    var parentList = childList.Cast<ParentType>().ToList();
    

    and pass that into the method, then you’ve got two entirely separate lists by that point anyway.

    By the way, another effect of the covariant of IEnumerable<T> is that in .NET 4 you can avoid the Cast call and just call:

    var parentList = childList.ToList<ParentType>();
    

    EDIT: Now that you’ve posted your code, it’s simply a matter of not calling the Cast method as a method:

    // This...
    helper(listB.Cast<P>.ToList())
    
    // should be this:
    helper(listB.Cast<P>().ToList())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
I have an abstract parent class Item , from which different types of items
I need a parent public abstract class which will have multiple child public static
I have one parent class, which is abstract class for now, for four different
I have a model that looks something like this: public abstract class Parent {
Let's say I have an abstract parent class called shape, and that there are
I want to have an abstract class with 3 classes extending it which i
In my c# project, I have two abstract generic parent classes which then have
Suppose I have two classes deriving from a third abstract class: public abstract class
I would like to have my abstract parent class have a method that would

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.