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
You won’t be able to cast a
List<T>directly topList<T>. You could make an extension method (just likeToList). Assuming your class has a constructor that takes anIEnumerable<T>to populate the list:If your class doesn’t have such a constructor, you can either add one, or do something like this:
First, if you have such a constructor, and you want to convert an existing
List<T>to apList<T>, you can of course do this: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
internalorpublicin, as appropriate:Also, if you want to use the extension method in a different namespace, you’ll have to have a
usingdirective in scope. For example:Elsewhere:
or