C# HashSet Overlaps

Description

HashSet Overlaps determines whether the current HashSet object and a specified collection share common elements.

Syntax

HashSet.Overlaps has the following syntax.


public bool Overlaps(
  IEnumerable<T> other
)

Parameters

HashSet.Overlaps has the following parameters.

  • other - The collection to compare to the current HashSet object.

Returns

HashSet.Overlaps method returns true if the HashSet object and other share at least one common element; otherwise, false.

Example

The following example creates two disparate HashSet objects and compares them to each another.


/*w  w w.java 2s .co  m*/
using System;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    HashSet<int> lowNumbers = new HashSet<int>();
    HashSet<int> allNumbers = new HashSet<int>();

    for (int i = 1; i < 5; i++)
    {
        lowNumbers.Add(i);
    }

    for (int i = 0; i < 10; i++)
    {
        allNumbers.Add(i);
    }

    Console.WriteLine("lowNumbers overlaps allNumbers: {0}",
        lowNumbers.Overlaps(allNumbers));

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack