Domanda

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?

È stato utile?

Soluzione

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);

Altri suggerimenti

  1. You are forgetting using System.Linq in your usages.
  2. Skip is LINQ method so you will need to convert to list after using skip with ToList() method.

The end result:

using System.Linq;

// some code

private static void SkipFirst(IList<float> list)
{
    IList<float> skippedFirst = list.Skip(1).ToList();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top