0

convert List<List<object>> to IList<IList<object>>

Does Enumerable.Cast<T> Copy Objects?

https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms132397(v=vs.110).aspx

My class, Person, implements my interface, ISequenced.

I have an ObservableCollection<Person> which I need to pass to a method which takes as a parameter an object of type IList<ISequenced> My question is how do I pass the ObservableCollection - without making a copy - such that the called function can modify it.

Note that the answer to this question: Is there a way to convert an observable collection to regular collection? demonstrates .ToList() which creates a new object.

public interface ISequenced
{
    int Sequence { get; set; }
}


public class Person : ISequenced
{
    public int Sequence {get; set;}
    //...
}

public class MyCode
{
    public ObservableCollection<Person> Entities {get; set;}
    public Person CurrentItem {get;set;}


    private void MoveDown()
    {
        IEnumerable<ISequenced> seq = Entities.Cast<ISequenced>(); // works but we need IList not IEnumerable
        IList<Person> p = (IList<Person>)Entities;                  //  works but we need IList ISequenced
        IList<ISequenced> pp = (IList<ISequenced>)p;                // throws

        // Need to pass a reference to Entities.... throws unable to cast here
        Sequencer.SequenceDown((IList<ISequenced>)Entities, CurrentItem as ISequenced);

    }
}

public static class Sequencer
{
    public static void SequenceDown(IList<ISequenced> items, ISequenced currentItem)
    {
        // swap the positions of two elements in the collection here
    }
}
Community
  • 1
  • 1
  • 1
    Well you can't _cast_ it, they're incompatible types. You'll have to convert it. – Jeff Mercado Feb 13 '16 at 21:00
  • @JeffMercado are you referring to the collection or my Person class? –  Feb 13 '16 at 21:03
  • The collection, as the answers in the questions you linked stated. – Jeff Mercado Feb 13 '16 at 21:03
  • @JeffMercado ObservableCollection derives from Collection which implements IList. What am I missing? I don't understand why they are incompatible. –  Feb 13 '16 at 21:05
  • `ObservableCollection` _does_ implement `IList`. However, `IList` isn't castable to `IList` because it's not a safe cast. – willaien Feb 13 '16 at 21:07
  • It's possible to this sort of thing with `IReadonlyList` because that interface is covariant. `IList` can't be covariant because then casts would allow you to put a `List` into a place where code was adding `Base` items into it. – Jon Hanna Feb 13 '16 at 21:15
  • 1
    FWIW, you don't really need to be able to do the cast or conversion in your case. It should be enough to just make `Sequencer.SequenceDown` generic. Something [like this](http://stackoverflow.com/a/29442087/390278). – Jeff Mercado Feb 13 '16 at 21:21
  • @JeffMercado Jeff you nailed it!! Thank you! Please post as answer and I will accept. –  Feb 13 '16 at 21:33

0 Answers0