.NET API Documentation

ISet<T> Interface

System.Collections.Generic
public interface ISet<T> : ICollection<T>

Represents a strongly typed collection of objects that is mathematically a set. A set is a collection that contains no duplicate elements and whose elements are in no particular order.

Remarks

The ISet<T> interface provides the basic methods for the operations that can be performed on a set. A set is a collection of elements that are unique. Duplicate elements are not allowed in a set.

The ISet<T> interface inherits from the ICollection<T> interface. Therefore, it includes the standard members for collections, such as Add, Remove, Contains, and Count.

ISet<T> adds methods to perform set operations such as UnionWith, IntersectWith, ExceptWith, and SymmetricExceptWith.

Supported by

The following .NET classes implement the ISet<T> interface:

Members

The ISet<T> interface defines the following members:

Methods

Name Description
Add(T element) Adds the specified element to the set.
UnionWith(IEnumerable<T> other) Modifies the current set so that it contains only elements that are present in both the current set and the specified collection.
IntersectWith(IEnumerable<T> other) Modifies the current set so that it contains only elements that are present in both the current set and the specified collection.
ExceptWith(IEnumerable<T> other) Modifies the current set so that it contains only elements that are present in the current set or in the specified collection but not in both.
SymmetricExceptWith(IEnumerable<T> other) Modifies the current set to contain elements that are present either in the current set or in the specified collection, but not in both.

Inherited Members from ICollection<T>

Name Description
Add(T element) Adds an item to the collection.
Clear() Removes all items from the collection.
Contains(T item) Determines whether the collection contains a specific value.
CopyTo(T[] array, int arrayIndex) Copies the collection to an array, starting at the specified array index.
Count Gets the number of elements contained in the collection.
IsReadOnly Gets a value indicating whether the collection is read-only.
Remove(T item) Removes the first occurrence of a specific object from the collection.

Example Usage


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        ISet<int> primeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 };
        ISet<int> moreNumbers = new HashSet<int> { 1, 2, 3, 4, 5 };

        // Union: {1, 2, 3, 4, 5, 7, 11, 13}
        primeNumbers.UnionWith(moreNumbers);
        Console.WriteLine("Union: " + string.Join(", ", primeNumbers));

        // Reset for next example
        primeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 };
        moreNumbers = new HashSet<int> { 1, 2, 3, 4, 5 };

        // Intersection: {2, 3, 5}
        primeNumbers.IntersectWith(moreNumbers);
        Console.WriteLine("Intersection: " + string.Join(", ", primeNumbers));

        // Reset for next example
        primeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 };
        moreNumbers = new HashSet<int> { 1, 2, 3, 4, 5 };

        // Except: {7, 11, 13}
        primeNumbers.ExceptWith(moreNumbers);
        Console.WriteLine("Except: " + string.Join(", ", primeNumbers));

        // Reset for next example
        primeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 };
        moreNumbers = new HashSet<int> { 1, 2, 3, 4, 5 };

        // Symmetric Except: {1, 4, 7, 11, 13}
        primeNumbers.SymmetricExceptWith(moreNumbers);
        Console.WriteLine("Symmetric Except: " + string.Join(", ", primeNumbers));
    }
}