두 목록을 비교하고 C#에서 세 번째 목록에 차이점을 추가하는 방법은 무엇입니까?

WBOY
풀어 주다: 2023-09-17 19:21:03
앞으로
698명이 탐색했습니다.

如何在 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");
로그인 후 복사

두 목록의 차이점을 찾아 차이점 요소를 표시합니다. -

IEnumerable < string > list3;
list3 = list1.Except(list2);
foreach(string value in list3) {
   Console.WriteLine(value);
}
로그인 후 복사

다음은 두 목록을 비교하는 완전한 예입니다. 예

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

public class Demo {
   public static void Main() {
      List < string > list1 = new List < string > ();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      list1.Add("D");

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

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

      list2.Add("C");
      list2.Add("D");
      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으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!