Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to get the first three elements from a list
The Take() method in C# is a LINQ extension method used to retrieve a specified number of elements from the beginning of a collection. It returns an IEnumerable<T> containing the first n elements from the original collection.
Syntax
Following is the syntax for using the Take() method −
IEnumerable<T> Take(int count)
Parameters
-
count − An integer specifying the number of elements to return from the start of the sequence.
Return Value
The Take() method returns an IEnumerable<T> that contains the specified number of elements from the start of the input sequence.
Using Take() Method with String List
Create a list of strings and use the Take() method to get the first three elements −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");
myList.Add("Five");
myList.Add("Six");
// Get first three elements
var res = myList.Take(3);
// Display the first three elements
foreach (string str in res) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
One Two Three
Using Take() Method with Integer List
The Take() method works with any collection type. Here's an example with integers −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50, 60, 70 };
// Get first three numbers
var firstThree = numbers.Take(3);
Console.WriteLine("First three numbers:");
foreach (int num in firstThree) {
Console.WriteLine(num);
}
}
}
The output of the above code is −
First three numbers: 10 20 30
Using Take() with Array
The Take() method also works with arrays since arrays implement IEnumerable<T> −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] fruits = { "Apple", "Banana", "Cherry", "Date", "Elderberry" };
// Get first three fruits
var firstThree = fruits.Take(3);
Console.WriteLine("First three fruits:");
foreach (string fruit in firstThree) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
First three fruits: Apple Banana Cherry
Key Rules
-
If the collection has fewer elements than the specified count,
Take()returns all available elements without throwing an exception. -
If count is zero or negative,
Take()returns an empty sequence. -
The method uses deferred execution − the elements are not retrieved until the result is enumerated.
Conclusion
The Take() method in C# provides a simple and efficient way to retrieve a specified number of elements from the beginning of any collection. It's particularly useful when you need to limit results or implement pagination in your applications.
