TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Skip, SkipWhile: Extension Methods

These C# example programs use the Skip and SkipWhile extension methods. They require the System.Linq namespace.

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. To start, you must include the System.Linq namespace. This provides the extension methods that act upon the IEnumerable implementations found in many common types such as arrays and Lists.

Tip: An extension method in the C# language is a static method that can be attached to a specific type to simplify the calling syntax.

Extension Method

C# program that uses Skip method from LINQ

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

In this example, 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.

Note: The argument to Skip is the int 2. This will pass over the first two elements and only return those that follow.

Lazy evaluation and Skip. Due to the design of LINQ, 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.

Loop 1: The loop prints out the third, fourth, fifth, and sixth elements in the array.

Loop 2: The second loop in the example repeats this procedure but for four elements.

SkipWhile skips over elements matching a condition. This method is provided in the System.Linq namespace for the C# language. With SkipWhile you need to specify a Func condition to skip over values with.

Here: To start, we declare an array that has several values in it. The first three elements are less than 10.

And: We call SkipWhile with a Func that returns true for elements less than 10. The first three elements in the array are skipped.

C# program that shows SkipWhile method

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	int[] array = { 1, 3, 5, 10, 20 };
	var result = array.SkipWhile(element => element < 10);
	foreach (int value in result)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

10
20

Practical uses. What is a practical usage of the SkipWhile method? In your program, you might have some reason to remove elements at the start of a collection that begin with a certain letter or have a property set to a certain value.

Tip: You could use SkipWhile. Then you could call ToArray to ToList to convert the IEnumerable back into an array or List.

ToArrayToListArraysList

Discussion. 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.

Generic MethodCompiler Phases

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.

For Loops

Tip: Often you would use Skip on a collection that cannot be accessed by specific indexes.

Combine Skip with Take. You can also use the Skip extension method in other query method chains in the C# language. Often you may want to combine it with Take, either before the Skip or after.

Note: The Take extension method specifies that you only want to access the following several elements.

So: If you want the middle elements in a sequence, you can Skip over the first several and then Take the part you want.

Take, TakeWhile

Summary. 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.

Review: We peeked at the implementation of Skip and compared it to the Take extension method. We also used SkipWhile.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf