Read the syntax
Calculate a small in-memory page while defining ordering and validating page inputs.
The calculated offset is two, so the first two items are skipped.
What it is and why to use it
Skip omits a number of elements and Take returns at most a specified count from the remainder. Together they express a window through an ordered sequence. They are convenient for a small local collection, but loading a huge database table and then applying in-memory pagination is not an efficient database paging strategy.
How it works
For one-based page numbers, the offset is (page - 1) * pageSize. Validate those inputs and consider overflow for large offsets. Enumerable operations are deferred and follow the supplied sequence’s order. Define a stable total order, including tie-breakers where needed, before relying on repeatable pages. Concurrent source changes can still alter page membership.
Common mistakes and limitations
Do not paginate an unordered source and expect a stable page. Do not confuse an offset with a record ID. An offset-based page over changing data can omit or repeat records across requests; a real service may need database-specific or keyset pagination.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
var pageItems = ordered.Skip(offset).Take(pageSize);Examples
Read the second page of a small list
Calculate the offset from fixed positive page values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
string[] lessons = { "Variables", "Arrays", "Functions", "Classes", "Dates" };
int page = 2;
int pageSize = 2;
int offset = checked((page - 1) * pageSize);
Console.WriteLine(string.Join(", ", lessons.Skip(offset).Take(pageSize)));
- The list’s current order is the intended learning order.
- The calculated offset is two, so the first two items are skipped.
- Take returns the next two items. Actual request input should be validated before calculating the offset.
- Expected output: Functions, Classes
- Validate page values and establish a stable ordering.
Order values before taking a window
Avoid depending on insertion order for a ranked display.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
int[] positions = { 5, 1, 4, 2, 3 };
var firstPage = positions.OrderBy(x => x).Skip(0).Take(2);
Console.WriteLine(string.Join(", ", firstPage));
Console.WriteLine(positions.Skip(99).Take(2).Count());
- OrderBy establishes ascending numeric order before the window is selected.
- The first page is therefore 1 and 2, not the first two inserted values.
- An offset beyond the sequence yields an empty result rather than inventing missing rows.
- Expected output: 1, 2 0
- Validate page values and establish a stable ordering.
Best practices
- Validate page values and establish a stable ordering.
- Perform large database paging at the appropriate query boundary.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference