直接上代码了,有两种合并的方式,一种是去重的合并,另一种是不去重的合并。

 static void Main(string[] args)
        {
            List<int> listA = new List<int> { 1, 4, 8, 9, 7, 8, 3 };
            List<int> listB = new List<int> { 13, 4, 17, 29, 2 };

            List<int> ResultA = listA.Union(listB).ToList<int>();          //剔除重复项
            List<int> ResultB = listA.Concat(listB).ToList<int>();          //保留重复项
            Console.Write("ResultA= ");
            foreach (int a in ResultA)
            {
                Console.Write(a+" ");
            }
            Console.WriteLine();
            Console.Write("ResultB= ");
            foreach (int a in ResultB)
            {
                Console.Write(a + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }

结果
在这里插入图片描述

API中还有一个寻找List中是否存在某个元素的方法,我在此也做一个记录

list.BinarySearch(元素);

更多推荐