Efficient List Intersection Checking in C# with LINQ: Performance Analysis and Best Practices

Dec 06, 2025 · Programming · 13 views · 7.8

Keywords: C# | LINQ | List Intersection | Performance Optimization | Any Method | Intersect Method

Abstract: This article explores various methods to check if list A contains any elements from list B in C#. By analyzing LINQ's Any() and Intersect() methods with performance test data, it reveals efficiency differences between implementations. The article explains method group syntax, deferred execution characteristics, and provides practical code examples to help developers choose optimal solutions for specific scenarios.

Introduction and Problem Context

In C# programming, it's common to determine whether two collections share any elements. For example, given list A = [1, 2, 3, 4] and list B = [2, 5], how can we efficiently check if A contains any value from B? While seemingly simple, different implementation approaches can yield significant performance variations.

Core Solution Analysis

Based on the best answer from the Q&A data, we first analyze two primary LINQ implementation approaches.

Method 1: Combining Any() with Contains()

The most intuitive approach uses the Any() extension method with Contains():

bool result = a.Any(item => b.Contains(item));

This method iterates through list A, checking each element for existence in list B. It returns true immediately upon finding a match, demonstrating short-circuit evaluation. C# also supports more concise method group syntax:

bool result = a.Any(b.Contains);

This syntax passes b.Contains directly as a delegate to Any(), resulting in cleaner code.

Method 2: Combining Intersect() with Any()

Another common approach first computes the intersection of both lists, then checks if it's non-empty:

bool result = a.Intersect(b).Any();

The Intersect() method uses hash set implementation with theoretical time complexity O(n+m), where n and m are the lengths of both lists. It returns a lazily evaluated enumerable sequence, with actual computation occurring only when Any() is called.

Performance Comparison and Test Data

The second answer in the Q&A provides crucial performance test results. In tests with 10 million iterations:

Tests indicate that for small lists (like the example with 4 and 2 elements), the Any(b.Contains) method is significantly faster. This occurs because:

  1. Intersect() requires creating additional hash set data structures
  2. For small datasets, hash set overhead may outweigh theoretical advantages
  3. Any(b.Contains) returns immediately upon finding the first match

Technical Details Deep Dive

Method Group Syntax Principles

The method group syntax a.Any(b.Contains) is a C# language feature where the compiler automatically converts it to:

a.Any(new Func<int, bool>(b.Contains))

This conversion happens at compile time, with runtime performance identical to the lambda expression version.

Deferred Execution Impact

LINQ's deferred execution characteristic manifests in both methods:

Practical Application Recommendations

Based on different usage scenarios, we recommend:

  1. Small lists or expected early matches: Use a.Any(b.Contains) to leverage short-circuit evaluation
  2. Need complete intersection results: If you'll need intersection elements later, use var intersection = a.Intersect(b) and cache the result
  3. Large datasets: For very large lists, Intersect()'s hash set implementation might offer advantages, but requires actual testing

Extended Considerations

This problem can be considered from additional perspectives:

Conclusion

Checking list intersections in C# offers multiple approaches, each suitable for different scenarios. Based on performance test data, a.Any(b.Contains) is generally optimal for common use cases, particularly with small lists or expected early matches. Developers should select implementations based on specific data characteristics and performance requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.