List
มาดูวิธีการใช้ Add() ใน C# กัน
ตัวอย่าง
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> sports = new List<string>();
sports.Add("Football");
sports.Add("Tennis");
sports.Add("Soccer");
foreach (string s in sports) {
Console.WriteLine(s);
}
}
} ผลลัพธ์
Football Tennis Soccer
ให้เราดูวิธีการใช้ Remove() วิธีการใน C#.
ตัวอย่าง
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> sports = new List<string>();
sports.Add("Football"); // add method
sports.Add("Tennis");
sports.Add("Soccer");
Console.WriteLine("Old List...");
foreach (string s in sports) {
Console.WriteLine(s);
}
Console.WriteLine("New List...");
sports.Remove("Tennis"); // remove method
foreach (string s in sports) {
Console.WriteLine(s);
}
}
} ผลลัพธ์
Old List... Football Tennis Soccer New List... Football Soccer