list2=newList();list2.Add("C");list2.Add(&quo"/> list2=newList();list2.Add("C");list2.Add(&quo">

 >  기사  >  백엔드 개발  >  C#에서 두 목록의 동등성을 비교하는 방법은 무엇입니까?

C#에서 두 목록의 동등성을 비교하는 방법은 무엇입니까?

WBOY
WBOY앞으로
2023-08-26 19:37:081649검색

如何在 C# 中比较两个列表是否相等?

두 개의 목록 설정 -

목록 1

List < string > list1 = new List < string > ();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");

목록 2

List < string > list2 = new List < string > ();
list2.Add("C");
list2.Add("D");

이제 다음이 다른 요소를 반환하면 목록이 동일하지 않음을 의미합니다. -

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

public class Demo {
   public static void Main() {
      List < string > list1 = new List < string > ();
      list1.Add("P");
      list1.Add("Q");
      list1.Add("R");

      Console.WriteLine("First list...");
      foreach(string value in list1) {
         Console.WriteLine(value);
      }

      Console.WriteLine("Second list...");
      List < string > list2 = new List < string > ();

      list2.Add("P");
      list2.Add("R");
      foreach(string value in list2) {
         Console.WriteLine(value);
      }

      Console.WriteLine("Difference in the two lists...");
      IEnumerable < string > list3;
      list3 = list1.Except(list2);
      foreach(string value in list3) {
         Console.WriteLine(value);
      }
   }
}

위 내용은 C#에서 두 목록의 동등성을 비교하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제