Jarmez De La Rocha
Jarmez De La Rocha

Reputation: 670

How can i convert to a sub type of IList<T> from List<T>

I have managed to create my own IList sub class using some code i found on another thread on the MSDN. I have added some of my own methods and have tested the class in basic scenarios and it seems to be working fine.

The problem is that when i try and use the regular .ToList() method i am returned a List instead of my custom pList. Obviously i need to cast it to my new type but i am unsure how. Do i need to implement another method in my custom iList to allow it to be assigned with a different format?

My class is declared as shown below.

public class pList<T> : IList<T>

James

Upvotes: 1

Views: 315

Answers (5)

Magnus
Magnus

Reputation: 46929

You need to create an extension method that returns your new list type

public static List<TSource> ToMyList<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw ArgumentNullException("source");
    }
    return new pList<TSource>(source);
}

Upvotes: 2

phoog
phoog

Reputation: 43046

You won't be able to cast a List<T> directly to pList<T>. You could make an extension method (just like ToList). Assuming your class has a constructor that takes an IEnumerable<T> to populate the list:

static class EnumerableExtensions
{
    static pList<T> ToPList<T>(this IEnumerable<T> sequence) { return new pList<T>(sequence); }
}

If your class doesn't have such a constructor, you can either add one, or do something like this:

static class EnumerableExtensions
{
    static pList<T> ToPList<T>(this IEnumerable<T> sequence)
    {
        var result = new pList<T>();
        foreach (var item in sequence)
            result.Add(item);
        return result;
    }
}

my pList class does have a constructor that takes IEnumerable have added your extension method but i am still unable to see ToPList() within the List Am i missing something?

First, if you have such a constructor, and you want to convert an existing List<T> to a pList<T>, you can of course do this:

List<T> originalList = GetTheListSomehow();
var newList = new pList<T>(originalList);

To use an extension method, you have to make sure that the method is in scope. I didn't add access modifiers to my example. Put internal or public in, as appropriate:

public static class EnumerableExtensions
{
    internal static pList<T> ToPList<T> //...

Also, if you want to use the extension method in a different namespace, you'll have to have a using directive in scope. For example:

namespace A { public static class EnumerableExtensions { ...

Elsewhere:

using A;
// here you can use the extension method

namespace B
{
    public class C
    {
        ...

or

namespace B
{
    using A;
    // here you can use the extension method

    public class C
    {
        ...

Upvotes: 4

user7116
user7116

Reputation: 64068

I'm not exactly sure what you're intending to accomplish, but perhaps you could add the following code:

// Constructor which handles enumerations of items
public pList(IEnumerable<T> items)
{
    // this.innerCollection = new Something(items);
}

Then with an extension method:

public static class pListExtensions
{
    public static pList<T> ToPList<T>(this IEnumerable<T> items)
    {
        return new pList<T>(items);
    }
}

Used later in your code:

var items = (from t in db.Table
             where condition(t)
             select new { Foo = bar(t), Frob = t.ToString() }).ToPList();

Upvotes: 4

Jacob
Jacob

Reputation: 995

You could also define an implicit cast.

public static implicit operator pList<T>(List<T> other)
{
     //Code returning a pList
}

Upvotes: 2

Justin Niessner
Justin Niessner

Reputation: 245429

IList<T> is an interface. Not a class. If you're treating your class as an instance of IList<T>, you can simply cast back rather than calling ToList():

// assume you're working with IList<string> instance = new pList<string>()
pList<string> castedBack = (pList<string>)instance;

Upvotes: 1

Related Questions