Toán tử Take dùng để trả về một số phần tử cho trước từ mảng và toán tử Skip bỏ qua một số phần tử chỉ định từ mảng.
Skip, bỏ qua các phần tử đến vị trí chỉ định bắt đầu từ phần tử đầu tiên trong chuỗi.
Take, lấy các phần tử đến vị trí chỉ định bắt đầu từ phần tử đầu tiên trong chuỗi.
Ví Dụ 1
class Program{
static void Main(string[] args){
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 7, 8, 8 };
System.Console.WriteLine(numbers.Count());
var skipRes = numbers.Skip(5);
System.Console.WriteLine(skipRes.Count());
Console.ReadLine();
}
}
Kết Quả
28 23
Ví Dụ 2
class Program{
static void Main(string[] args){
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 7, 8, 8 };
System.Console.WriteLine(numbers.Count());
var takeRes = numbers.Take(5);
System.Console.WriteLine(takeRes.Count());
Console.ReadLine();
}
}
Kết Quả
28 5
Ví Dụ 3
class Program{
static void Main(string[] args){
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, 7, 8, 8 };
System.Console.WriteLine(numbers.Count());
var takeSkipRes = numbers.Skip(10).Take(18);
System.Console.WriteLine(takeSkipRes.Count());
Console.ReadLine();
}
}
Kết Quả
28 18