Intersect() method in C# is an extension method.It applies the set theory of mathematics.In set theory the Intersect method returns the common elements of two sets.
Similarly the Intersect method returns the common elements in two collections.It is defined in the namespace System.Linq
For example if we define two lists as:
IList<int> intList1 = new List<int>() { 100, 200, 300, 400 }; IList<int> intList2 = new List<int>() { 100, 500, 600 };
then we can find the common elements in two lists as:
var commonElements = intList1.Intersect(intList2); foreach (var item in commonElements) Console.WriteLine(item);
the above program will output the value 100.Since 100 is common element in the two lists it is returned by the Intersect method.
Intersect method accepts a single argument which is IEnumerable<T>.
Important point is that IEnumerable<T> argument should be of same type as the collection on which Intersect method is called.
So if you try to invoke the Intersect method as:
IList<string> strList = new List<string>() { "" }; var commonElements = intList1.Intersect(strList);
then you will get compilation error since the types of intList1 and strList are different.