Computer >> Máy Tính >  >> Lập trình >> C#

Làm cách nào để bật phần tử đầu tiên khỏi Danh sách C #?


Để bật phần tử đầu tiên trong danh sách, hãy sử dụng phương thức RemoveAt (). Nó loại bỏ phần tử khỏi vị trí bạn muốn xóa phần tử.

Đặt danh sách

List<string> myList = new List<string>() {
   "Operating System",
   "Computer Networks",
   "Compiler Design"
};

Bây giờ, hãy bật phần tử đầu tiên bằng cách sử dụng RemoveAt (0)

myList.RemoveAt(0);

Hãy để chúng tôi xem ví dụ đầy đủ.

Ví dụ

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

class Program {
   static void Main() {
      List<string> myList = new List<string>() {
         "Operating System",
         "Computer Networks",
         "Compiler Design"
      };

      Console.Write("Initial list...");
      foreach (string list in myList) {
         Console.WriteLine(list);
      }

      Console.Write("Removing first element from the list...");
      myList.RemoveAt(0);

      foreach (string list in myList) {
         Console.WriteLine(list);
      }
   }
}

Đầu ra

Initial list...
Operating System
Computer Networks
Compiler Design
Removing first element from the list...
Computer Networks
Compiler Design