ISet<T> Interface

Namespace: System.Collections.Generic

Assembly: System.Runtime.dll

Overview

Represents a collection of non-unique elements that can be accessed by index.

The ISet<T> interface is the base interface for all generic set types. A set is a collection that contains no duplicate values. It is a generic version of the ISet interface.

Members

The ISet<T> interface defines the following methods:

Member Description
Add(T item) Adds the specified element to the set.
ExceptWith(IEnumerable<T> other) Modifies the current set so that it contains only elements that are present in that set and in the specified collection.
IntersectWith(IEnumerable<T> other) Modifies the current set so that it contains only elements that are present in both the set and the specified collection.
IsProperSubsetOf(IEnumerable<T> other) Determines whether the current set is a proper subset of a specified collection.
IsProperSupersetOf(IEnumerable<T> other) Determines whether the current set is a proper superset of a specified collection.
IsSubsetOf(IEnumerable<T> other) Determines whether the current set is a subset of a specified collection.
IsSupersetOf(IEnumerable<T> other) Determines whether the current set is a superset of a specified collection.
Overlaps(IEnumerable<T> other) Determines whether the current set overlaps with the specified collection.
SetEquals(IEnumerable<T> other) Determines whether the current set and the specified collection contain the same elements.
SymmetricExceptWith(IEnumerable<T> other) Modifies the current set so that it contains only elements that are present in either the set or the specified collection, but not in both.
UnionWith(IEnumerable<T> other) Modifies the current set so that it contains all elements that are present in either the set or the specified collection.

Requirements

Supported in the following .NET Framework versions:

  • .NET Framework 4.0
  • .NET Framework 4.5
  • .NET Framework 4.5.1
  • .NET Framework 4.5.2
  • .NET Framework 4.6
  • .NET Framework 4.6.1
  • .NET Framework 4.6.2
  • .NET Framework 4.7
  • .NET Framework 4.7.1
  • .NET Framework 4.7.2
  • .NET Framework 4.8
  • .NET Core 1.0
  • .NET Core 1.1
  • .NET Core 2.0
  • .NET Core 2.1
  • .NET Core 2.2
  • .NET Core 3.0
  • .NET Core 3.1
  • .NET 5
  • .NET 6
  • .NET 7
  • .NET 8

Thread Safety

Public static members of this type are thread-safe. Any instance members are not guaranteed to be thread-safe.

Implementation Notes

The ISet<T> interface is implemented by classes such as HashSet<T> and SortedSet<T>. These classes provide specific performance characteristics and ordering behaviors.

Exceptions

Methods that modify the set may throw exceptions under certain conditions, such as when trying to add duplicate elements (if not explicitly handled by the implementation) or when the collection is modified during iteration.

Remarks

A set typically uses a hash table for efficient lookups, insertions, and deletions. The time complexity for these operations is generally O(1) on average, but can degrade to O(n) in the worst case due to hash collisions.

When comparing elements, the default equality comparer for the type T is used. You can provide a custom equality comparer to the implementing collection class if needed.

Examples

Creating and Using a HashSet

The following example demonstrates how to create a HashSet<T> and use some of its common set operations.


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create two hash sets.
        var setA = new HashSet<int>();
        setA.Add(1);
        setA.Add(2);
        setA.Add(3);

        var setB = new HashSet<int>();
        setB.Add(2);
        setB.Add(3);
        setB.Add(4);

        Console.WriteLine("Set A: { " + string.Join(", ", setA) + " }");
        Console.WriteLine("Set B: { " + string.Join(", ", setB) + " }");

        // Perform set operations.
        setA.UnionWith(setB);
        Console.WriteLine("Union with B: { " + string.Join(", ", setA) + " }"); // Output: Union with B: { 1, 2, 3, 4 }

        // Reset setA for next operation
        setA.Clear();
        setA.Add(1);
        setA.Add(2);
        setA.Add(3);

        setA.IntersectWith(setB);
        Console.WriteLine("Intersection with B: { " + string.Join(", ", setA) + " }"); // Output: Intersection with B: { 2, 3 }

        // Reset setA for next operation
        setA.Clear();
        setA.Add(1);
        setA.Add(2);
        setA.Add(3);

        setA.ExceptWith(setB);
        Console.WriteLine("Except with B: { " + string.Join(", ", setA) + " }"); // Output: Except with B: { 1 }

        // Reset setA for next operation
        setA.Clear();
        setA.Add(1);
        setA.Add(2);
        setA.Add(3);

        setA.SymmetricExceptWith(setB);
        Console.WriteLine("Symmetric Except with B: { " + string.Join(", ", setA) + " }"); // Output: Symmetric Except with B: { 1, 4 }
    }
}