I have IPagedList interface which implements IList like this
public interface IPagedList<T> : IList<T>
{
int PageCount { get; }
int TotalItemCount { get; }
int PageIndex { get; }
...
}
Now I want to use this IPagedList to represent data with pagination.
I’m trying to convert existing code which was using IList to IPagedList
public IList<MyViewModel> Data
{
get
{
List<MyViewModel> myVal = new List<MyViewModel>();
foreach (MyModel m in data)
{
myVal.Add(new MyViewModel(m));
}
return myVal;
}
}
So, I’m wondering why this cannot be converted to
public IPagedList<MyViewModel> Data
{
get
....
cause IPagedList implements IList and how can I fix this.
IPagedList<T>implementsIList<T>andList<T>also implementsIList<T>, that’s correct.But that doesn’t mean that a
List<T>can be converted into anIPagedList<T>. That are still two different types. Think about it: A Porsche is a car, a Peugeot is a car, still they aren’t interchangable.You need to have a concrete implementation of the
IPagedList<T>interface. If you have one, you can use this code: