Search code examples
c#monoienumerableilist

IEnumerable interface and Skip method in Mono


I'm looking for the Skip method of IEnumerable interface.

IList public IEnumerable interface, so I thought I was able to access that method.

Anyway the following code fails to compile on Mono:

private static void SkipFirst(IList<float> list)
{
    IList<float> skippedFirst = list.Skip(1);
}

Here's the error:

error CS1061: Type System.Collections.Generic.IList<float>' does not contain a definition forSkip' and no extension method Skip' of type System.Collections.Generic.IList' could be found (are you missing a using directive or an assembly reference?)

What am I doing wrong? How can I access that method from an IList interface?


Solution

  • Skip is an extension method, so it is not actually a method of any of the collection classes. Instead it is defined by Linq. You need to add a reference to Linq: using System.Linq;

    list.Skip(1) is just syntactical sugar, it is actually converted to Enumerable.Skip(list, 1)

    Also, Skip returns IEnumerable<T> which means you need to use either

    IList<float> skippedFirst = list.Skip(1).ToList();
    

    or

    IEnumerable<float> skippedFirst = list.Skip(1);