C# HashSet SetEquals

Description

HashSet SetEquals determines whether a HashSet object and the specified collection contain the same elements.

Syntax

HashSet.SetEquals has the following syntax.


public bool SetEquals(
  IEnumerable<T> other
)

Parameters

HashSet.SetEquals has the following parameters.

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

Returns

HashSet.SetEquals method returns true if the HashSet object is equal to other; otherwise, false.

Example

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


//from   w w  w.j a v a  2  s  .  c  o 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(allNumbers.SetEquals(lowNumbers));

  }
}
    

The code above generates the following result.





















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




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack