c#两个LIST对象移除相同的
您可以使用LINQ的Except方法来实现移除相同元素的功能。下面是一个示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
List<int> list2 = new List<int> { 4, 5, 6, 7, 8 };
List<int> result = list1.Except(list2).ToList();
Console.WriteLine("List 1:");
foreach (int num in list1)
{
Console.Write(num + " ");
}
Console.WriteLine("\nList 2:");
foreach (int num in list2)
{
Console.Write(num + " ");
}
Console.WriteLine("\nResult:");
foreach (int num in result)
{
Console.Write(num + " ");
}
Console.ReadLine();
}
}
输出结果为:
List 1:
1 2 3 4 5
List 2:
4 5 6 7 8
Result:
1 2 3
``
原文地址: https://www.cveoy.top/t/topic/hJYr 著作权归作者所有。请勿转载和采集!