Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List.Cast<> error "is a method which is not valid in the given context"

Tags:

c#

casting

linq

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) {
        //...
    }
}
like image 621
David Avatar asked May 31 '12 19:05

David


2 Answers

Now that you've added code, I see two potential problems:

  1. You need to add parentheses when calling Cast e.g.

    listA.Cast<P>()

    Cast is not some special operator, it's an extension method like anything else.

  2. Are those calls to helper actually at the class level, and not inside another method? That would be a problem too.

like image 23
Paul Phillips Avatar answered Sep 27 '22 02:09

Paul Phillips


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())
like image 72
Jon Skeet Avatar answered Sep 26 '22 02:09

Jon Skeet