Enumerable::Intersect<TSource> Method (IEnumerable<TSource>^, IEnumerable<TSource>^, IEqualityComparer<TSource>^)
Produces the set intersection of two sequences by using the specified IEqualityComparer<T> to compare values.
Assembly: System.Core (in System.Core.dll)
public: generic<typename TSource> [ExtensionAttribute] static IEnumerable<TSource>^ Intersect( IEnumerable<TSource>^ first, IEnumerable<TSource>^ second, IEqualityComparer<TSource>^ comparer )
Parameters
- first
-
Type:
System.Collections.Generic::IEnumerable<TSource>^
An IEnumerable<T> whose distinct elements that also appear in second will be returned.
- second
-
Type:
System.Collections.Generic::IEnumerable<TSource>^
An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.
- comparer
-
Type:
System.Collections.Generic::IEqualityComparer<TSource>^
An IEqualityComparer<T> to compare values.
Return Value
Type: System.Collections.Generic::IEnumerable<TSource>^A sequence that contains the elements that form the set intersection of two sequences.
Type Parameters
- TSource
The type of the elements of the input sequences.
| Exception | Condition |
|---|---|
| ArgumentNullException | first or second is null. |
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no other elements.
When the object returned by this method is enumerated, Intersect<TSource> yields distinct elements occurring in both sequences in the order in which they appear in first.
If comparer is null, the default equality comparer, Default, is used to compare values.
The following example shows how to implement an equality comparer that can be used in the Intersect<TSource> method.
After you implement this comparer, you can use sequences of Product objects in the Intersect<TSource> method, as shown in the following example.
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1