Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Chương trình C# lấy ba phần tử đầu tiên từ danh sách

Phương thức Take(int count) trả về số phần tử đầu tiên của sequence.

Ví dụ

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

public class Demo
{
    public static void Main()
    {
        List myList = new List();
        myList.Add("One");
        myList.Add("Two");
        myList.Add("Three");
        myList.Add("Four");
        myList.Add("Five");
        myList.Add("Six");

        var res = myList.Take(3);
        foreach (string str in res)
            Console.WriteLine(str);
    }
}

Kết quả

One
Two
Three

Tương tự: Skip(n) bỏ qua n phần tử đầu.