Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C# 목록에서 Add, Remove 메서드란 무엇입니까?

<시간/>

List는 C#의 컬렉션이며 일반 컬렉션입니다. add 및 remove 메소드는 요소를 추가 및 제거하기 위해 C# 목록에서 사용됩니다.

C#에서 Add() 메서드를 사용하는 방법을 알아보겠습니다.

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

C#에서 Remove() 메서드를 사용하는 방법을 살펴보겠습니다.

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