C# Program to return specified number of elements from the beginning of a sequence

The Take() method in C# is a LINQ extension method that returns a specified number of elements from the beginning of a sequence. This method is particularly useful when you need to retrieve only the first few elements from a collection, especially after sorting or filtering operations.

Syntax

Following is the syntax for the Take() method −

public static IEnumerable<TSource> Take<TSource>(
    this IEnumerable<TSource> source,
    int count
)

Parameters

  • source − The sequence to return elements from.

  • count − The number of elements to return from the beginning of the sequence.

Return Value

Returns an IEnumerable<T> containing the specified number of elements from the start of the input sequence.

Take() Method Operation Original Sequence [290, 340, 129, 540, 456] Take(3) Result [290, 340, 129]

Using Take() with Sorted Arrays

Example

This example demonstrates using Take() with OrderByDescending() to get the top elements −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      int[] prod = { 290, 340, 129, 540, 456, 898, 765, 789, 345};
      
      // Get top two products by value
      IEnumerable<int> units = prod.AsQueryable().OrderByDescending(s => s).Take(2);
      
      Console.WriteLine("Top 2 products:");
      foreach (int res in units) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Top 2 products:
898
789

Using Take() with Unsorted Arrays

Example

This example shows Take() returning elements from the beginning without sorting −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      string[] names = {"Alice", "Bob", "Charlie", "Diana", "Eve"};
      
      // Take first 3 names
      IEnumerable<string> firstThree = names.Take(3);
      
      Console.WriteLine("First 3 names:");
      foreach (string name in firstThree) {
         Console.WriteLine(name);
      }
   }
}

The output of the above code is −

First 3 names:
Alice
Bob
Charlie

Using Take() with Where() Condition

Example

This example combines Where() filtering with Take() to get specific elements −

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      int[] numbers = {12, 45, 78, 23, 56, 89, 34, 67, 90, 11};
      
      // Take first 3 numbers greater than 50
      IEnumerable<int> result = numbers.Where(x => x > 50).Take(3);
      
      Console.WriteLine("First 3 numbers greater than 50:");
      foreach (int num in result) {
         Console.WriteLine(num);
      }
   }
}

The output of the above code is −

First 3 numbers greater than 50:
78
56
89

Key Features

Feature Description
Deferred Execution The query is not executed until enumerated (foreach, ToList(), etc.)
Works with Any IEnumerable Can be used with arrays, lists, and other enumerable collections
Safe for Small Collections If count exceeds collection size, returns all available elements
Chainable Can be combined with other LINQ methods like Where(), OrderBy()

Conclusion

The Take() method is an essential LINQ extension that efficiently retrieves a specified number of elements from the beginning of any sequence. It's commonly used with sorting and filtering operations to implement "top N" scenarios and pagination features in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements