SoFunction
Updated on 2025-03-06

C#'s operation method for multiple collections and arrays (merge, deduplication, judgment)

During the development process, the processing of arrays and collections is the most worrying thing about us. We generally use for or foreach to handle some operations. Here we introduce some commonly used sets and array operation functions.

First, let’s give examples of two sets A and B.

List<int> listA = new List<int> {1,2,3,5,7,9};

List<int> listB = new List<int> {13,4,17,29,2};

(listB ); Merge the sets

List<int> Result = (listB).ToList<int>(); //Exclude duplicates

List<int> Result = (listB).ToList<int>(); //Keep duplicates

("1"); //Judge whether a value is included in the collection. If it is included, return 0

Two arrays

int[] i=new int[]{1,2};
int[] j=new int[]{2,3};
List<int> r = new List<int>();
(i);

(j);
int[] c = (); Merge arrays

int[] x=(j).ToArray<int>(); //Exclude duplicates

int[] x=(j).ToArray<int>(); //Keep duplicates

int n = (i,3);//Judge whether a value is included in the array. If it is included, it returns 0

The above C# operation methods (merge, deduplication, judgment) on multiple sets and arrays are all the content I share with you. I hope you can give you a reference and I hope you can support me more.