C# Skip

Skip illustration

Skip is an extension method. It returns only the elements following a certain number of initial elements that must be ignored. It is found in the System.Linq namespace. It is useful in programs that selectively process elements.

Example

Note

First here we note that you must include the System.Linq namespace to use the extension methods that act upon the IEnumerable implementations in many common types such as arrays and Lists. An extension method in the C# programming language is a static method that can be attached to a specific type to simplify the calling syntax.

Extension Method

This C# example program uses the Skip extension method. It requires System.Linq.

Program that uses Skip method from LINQ [C#]

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	//
	// Array that is IEnumerable for demonstration.
	//
	int[] array = { 1, 3, 5, 7, 9, 11 };
	//
	// Get collection of all elements except first two.
	//
	var items1 = array.Skip(2);
	foreach (var value in items1)
	{
	    Console.WriteLine(value);
	}
	//
	// Call Skip again but skip the first four elements this time.
	//
	var items2 = array.Skip(4);
	foreach (var value in items2)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

5      The first two numbers in the array are missing.
7
9
11

9      The first four numbers are skipped.
11
Main method

Description. In the program the Main entry point is defined—this is where control flow begins in the execution engine. The integer array is allocated and immediately filled with six 32-bit signed integers. Next, the implicitly typed variable with the identifier items1 is assigned to the result of the Skip extension method. The parameter to Skip is the integer value 2, which indicates that you want to pass over the first two elements and only return those that follow.

LINQ (language integrated query)

Lazy evaluation and Skip. Due to the design of the LINQ extension methods, no action is taken until you actually loop over the resulting variable. In this example, Skip takes effect when the foreach-loop queries the items1 variable for the first results. The loop prints out the third, fourth, fifth, and sixth elements in the array only. The second loop in the example repeats this procedure but for four elements.

Implementation

.NET Framework information

The Skip extension method is defined in the System.Linq namespace, which is physically located in the System.Core.dll. It is a generic method, which means it requires a type parameter. The C# compiler can infer the correct type parameter so you do not have to specify it in sharp brackets normally.

The Skip extension method internally allocates a new class that is compiler-generated and stores the state of the Skip operation. This indicates that using a for-loop and starting at the desired index will be much faster. Often you would use Skip on a collection that cannot be accessed by specific indexes.

For LoopsProgramming tip

Combine Skip with Take. You can also use the Skip extension method in other query method chains in the C# programming language. Often you may want to combine it with Take, either before the Skip or after. The Take extension method specifies that you only want to access the following several elements. If you want the middle elements in a sequence, you can Skip over the first several and then Take the part you want.

Take

Summary

The C# programming language

Skip serves to "pass over" several elements in any class that implements the IEnumerable interface. It is most useful on more complex collections or queries. It is implemented in System.Core.dll from the .NET Framework. We peeked at the implementation of Skip and compared it to the Take extension method.

LINQ Examples
.NET